一、简介
公司最近准备用flutter来写web端页面,需要做ui自动化测试,由于flutter的页面元素无法定位,不能用selenium+unittest的方法进行,只能寻求其它的自动化方法,这时发现flutter录制脚本可以录制操作,然后自动生成脚本文件,然后运行脚本文件就可以实现了。
Playwright是微软推出来的一款自动化测试工具
介绍
Playwright enables fast, reliable and capable automation across all modern browsers.
支持平台&浏览器
Linux | macOS | Windows | |
---|---|---|---|
Chromium 89.0.4344.0 | ✅ | ✅ | ✅ |
WebKit 14.1 | ✅ | ✅ | ✅ |
Firefox 84.0b9 | ✅ | ✅ | ✅ |
支持语言
- JavaScript and TypeScript: https://github.com/microsoft/playwright
- Java: https://github.com/microsoft/playwright-java
- Python: https://github.com/microsoft/playwright-python
- C#: https://github.com/microsoft/playwright-sharp
二、安装
第 1 步,安装 playwright-python
pip install playwright
第 2 步,安装的浏览器驱动,会将 Chromeium、Firefox、Webkit 浏览器驱动下载到本地
python -m playwright install
安装完成后,可以查看课时使用的命令
python -m playwright help
C:UsersAdministrator>python -m playwright help Usage: npx playwright [options] [command] Options: -V, --version output the version number -h, --help display help for command Commands: open [options] [url] open page in browser specified via -b, --browser codegen [options] [url] open page and generate code for user ac tions debug <app> [args...] run command in debug mode: disable time out, open inspector install [browser...] ensure browsers necessary for this vers ion of Playwright are installed install-deps [browser...] install dependencies necessary to run b rowsers (will ask for sudo permissions) cr [options] [url] open page in Chromium ff [options] [url] open page in Firefox wk [options] [url] open page in WebKit screenshot [options] <url> <filename> capture a page screenshot pdf [options] <url> <filename> save page as pdf show-trace [options] [trace] Show trace viewer help [command] display help for command
三、录制脚本
- python -m playwright codegen 录制脚本
- –help 帮助文档
- -o 生成自动化脚本的目录(文件默认生成的地址为你cmd运行命令的地址,也可以在命令中输入需要保存的地址)
- –target 脚本语言,包含 JS 和 Python,分别对应值为:python 和 javascript
- -b 指定浏览器驱动
在cmd中执行:
python -m playwright codegen --target python -o test.py -b chromium https://www.baidu.com
命令运行后会自动打开浏览器,同时会打开一个Inspector ,所有的操作都会自动生成脚本显示在Inspector 里面,关闭浏览器这是脚本会自动保存
可以查看保存的py文件
1 from playwright.sync_api import Playwright, sync_playwright 2 3 4 def run(playwright: Playwright) -> None: 5 browser = playwright.chromium.launch(headless=False) 6 context = browser.new_context() 7 8 # Open new page 9 page = context.new_page() 10 11 # Go to https://www.baidu.com/ 12 page.goto("https://www.baidu.com/") 13 14 # Click input[name="wd"] 15 page.click("input[name="wd"]") 16 17 # Fill input[name="wd"] 18 page.fill("input[name="wd"]", "测试") 19 20 # Press Enter 21 # with page.expect_navigation(url="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E6%B5%8B%E8%AF%95&fenlei=256&rsv_pq=e29e95d200057ece&rsv_t=6332vNWuKAWk0z%2Be3iFDs%2Fy16SkgQUYh7xVJHgu6DzflmHOv%2F4n2clPFJwY&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=7&rsv_sug1=5&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=%25E6%25B5%258B%25E8%25AF%2595&rsp=5&inputT=868&rsv_sug4=1743&rsv_jmp=fail"): 22 with page.expect_navigation(): 23 page.press("input[name="wd"]", "Enter") 24 # assert page.url == "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E6%B5%8B%E8%AF%95&fenlei=256&rsv_pq=e29e95d200057ece&rsv_t=6332vNWuKAWk0z%2Be3iFDs%2Fy16SkgQUYh7xVJHgu6DzflmHOv%2F4n2clPFJwY&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=7&rsv_sug1=5&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=%25E6%25B5%258B%25E8%25AF%2595&rsp=5&inputT=868&rsv_sug4=1743" 25 26 # Close page 27 page.close() 28 29 # --------------------- 30 context.close() 31 browser.close() 32 33 34 with sync_playwright() as playwright: 35 run(playwright)
同时还支持同步和异步的操作