1. 安装 pytest
首先,你需要确保你的环境中安装了 pytest。如果没有安装,可以通过以下命令安装:
pip install pytest
2. 安装 Playwright 及其 pytest 插件
为了使用 Playwright 进行测试,你需要安装 Playwright 和 pytest-playwright 插件。运行以下命令来安装:
pip install playwright pytest-playwright
3. 安装浏览器
Playwright 支持多个浏览器(Chromium、Firefox、WebKit)。运行以下命令安装浏览器:
playwright install
4. 编写测试用例
在项目目录下创建一个测试文件,例如 test_example.py,并编写如下内容:
1 2 3 4 5 6 7 8 9 |
def test_example(page): # 打开页面 page.goto("https://example.com") # 验证页面标题 assert page.title() == "Example Domain" |
5. 运行测试
使用 pytest 来运行测试用例:
pytest
6. 运行时选项
你可以使用一些选项来选择运行的浏览器,例如运行所有支持的浏览器:
pytest --browser chromium --browser firefox --browser webkit
这样就可以通过 pytest 运行 Playwright 测试了,整个环境的安装与配置也会非常顺畅。
原创文章,作者:iTestCat,保留所有权利,禁止转载,如若转载,请联系作者!