Playwright安装及基本用法
作者:互联网
优点:
1、适用于多浏览器、多语言
2、可以使用基于css、xpath、text这些常用的元素定位方式进行录制生成代码,能大幅度的减少写代码的时间,同时代码稳定性也可以保证
3、可以使用api方式操作浏览器内核,速度快,而且可以和浏览器双向沟通,元素操作可以在元素ready的情况下进行,稳定性很好
4、支持UI和非UI方式运行,节省了debug、 run时间
5、与pytest测试框架集成
UI测试框架比较可参考:
虫师的博文:https://www.cnblogs.com/fnng/p/14230840.html
或者https://www.testim.io/blog/puppeteer-selenium-playwright-cypress-how-to-choose/
重要的截图看下:
安装
1、安装playwright库(需要python3.7+)
pip install playwright
2、安装浏览器驱动文件(自动安装Chromium、Firefox、WebKit等浏览器的驱动文件)
python -m playwright install
录制
录制命令codegen
通过python -m playwright codegen --help 可查看所有用法
options含义:
-o:将录制的脚本保存到一个文件的文件名称
--target:指定生成的脚本语言,默认为python,目前支持python和JS
-b:指定浏览器驱动
例如:
使用谷歌浏览器打开百度搜索,脚本文件名称为open_baidu.py,脚本语言为python
命令是:python -m playwright codegen --target python -o open_baidu.py -b chromium https://www.baidu.com
命令行输入后会自动打开浏览器,然后在浏览器上的一举一动都会被自动翻译成代码,结束后自动关闭浏览器,保存生成自动化脚本到py文件
执行命令后如下图:
继续操作,输入关键字“三星堆”,点击百度一下,再关闭浏览器,生成脚本如下:
源码:
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://www.baidu.com/
page.goto("https://www.baidu.com/")
# Click input[name="wd"]
page.click("input[name=\"wd\"]")
# Fill input[name="wd"]
page.fill("input[name=\"wd\"]", "三星堆")
# Click text=百度一下
page.click("text=百度一下")
# assert page.url == "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%B8%89%E6%98%9F%E5%A0%86&fenlei=256&rsv_pq=f1b6d9eb0013cce4&rsv_t=7ce127Agtx9HVMNsGe8C3CFkBbc%2FkR2ZyMvDG%2B0UkRXKIQJ5bbY3zm6BO44&rqlang=cn&rsv_enter=1&rsv_dl=ib&rsv_sug3=11&rsv_sug1=2&rsv_sug7=100"
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
支持同步:
源码1:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.baidu.com")
print(page.title())
page.screenshot(path=f'example.png')
browser.close()
源码2(参考链接:http://www.51ste.com/share/det-5936-2.html):from time import sleep
from playwright.sync_api import sync_playwright支持异步: 源码1:
# 注意:默认是无头模式
with sync_playwright() as p:
# 分别对应三个浏览器驱动
for browser_type in [p.chromium, p.firefox, p.webkit]:
# 指定为有头模式,方便查看
browser = browser_type.launch(headless=False)
page = browser.new_page()
page.goto('http://baidu.com')
# 执行一次搜索操作
page.fill("input[name=\"wd\"]", "自动化测试实战宝典")
with page.expect_navigation():
page.press("input[name=\"wd\"]", "Enter")
# 等待页面加载完全
page.wait_for_selector("text=搜索工具")
# 截图
page.screenshot(path=f'test-{browser_type.name}.png')
# 休眠3s
sleep(3)
# 关闭浏览器
browser.close()
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.new_page()
await page.goto('https://www.baidu.com/')
await page.screenshot(path=f'example-{browser_type.name}.png')
print(await page.title())
await browser.close()
#asyncio.run(main())
源码2:
import asyncio
from playwright.async_api import async_playwright
from time import sleep
# 异步执行
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
# 指定为有头模式,方便查看
browser = await browser_type.launch(headless=False)
page = await browser.new_page()
await page.goto('https://www.baidu.com')
# 执行一次搜索操作
await page.fill("input[name=\"wd\"]", "自动化测试实战宝典")
await page.press("input[name=\"wd\"]", "Enter")
# 等待页面加载完全
await page.wait_for_selector("text=搜索工具")
# 等待3秒
sleep(3)
# 截图
await page.screenshot(path=f'test-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
同步和异步的注意包的引入,否则会报错:Cannot find reference 'async_playwright' in '__init__.py'
支持移动端
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.new_context(
**iphone_11,
locale='en-US',
geolocation={'longitude': 12.492507, 'latitude': 41.889938},
permissions=['geolocation']
)
page = context.new_page()
page.goto('https://maps.google.com')
page.click('text="Your location"')
page.screenshot(path='colosseum-iphone.png')
browser.close()
支持pytest测试框架
参考文章:
https://blog.csdn.net/tomoya_chen/article/details/115648583
https://github.com/microsoft/playwright-pytest
更多功能参考官方文档:https://playwright.dev/python/docs/intro/
参考链接:
https://www.imooc.com/article/314264
http://quan.51testing.com/pcQuan/article/116997
http://www.51ste.com/share/det-5936-1.html
https://blog.csdn.net/tomoya_chen/article/details/115648583
标签:playwright,com,await,用法,Playwright,https,安装,page,browser 来源: https://www.cnblogs.com/sanyvaelailai/p/14688189.html