Web自动化(Python语言)
CukeTest暂时还未集成Python版本的Playwright,因此如果需要录制生成Web自动化的Python代码,需要手动下载Playwright。
下载Playwright
配合pytest使用Playwright,安装pytest-playwright
模块:
pip install pytest-playwright
接着执行命令安装相应的浏览器:
playwright install
使用Playwright
最常用的场景是录制生成Web自动化代码,可以使用下面的命令开始录制:
playwright codegen https://bing.com --target=python -o recording.py -b cr
--target
:表示生成哪种语言的自动化代码,这里生成了python代码,默认为Playwright自己定义的测试代码。-o
:表示录制代码输出到哪个文件,默认不输出。-b
:表示使用哪个浏览器录制,默认为Chromium浏览器,可选项还有火狐ff
、WebKitwk
。
具体的用法可以在命令行中使用-h
选项查看帮助:
playwright codegen -h
运行脚本
假设录制生成的文件名为recording.py
,那么直接运行python recording.py
即可。
也可以使用pytest运行,比如运行一个测试文件:
pytest ./recording.py
或者运行一个测试文件夹:
pytest .
又或者是运行满足条件的测试方法:
pytest -k "test_add_a_todo_item"