【导语】:Playwright 是微软开源的一款功能强大的自动化端到端测试工具,使 UI 交互自动化,并且反对浏览器端的自动化脚本录制。
简介
Playwright 是微软开源的一款功能强大的自动化测试工具,目前有 4 个版本,别离反对 JavaScript(TypeScript)、Python、C# 和 Go 语言,JavaScript 版本目前曾经在 GitHub 上播种了 1.8 万的 Star,这篇文章介绍的是 Python 版本,要求 Python 版本为 3.7+。几个版本性能是统一的,只是脚本语言不同而已。
Playwright 反对有头(headful)模式和无头(headless)模式运行。目前反对支流的浏览器,包含 Chromium 内核的浏览器以及 Webkit、Firefox 浏览器。
性能特点:
1、录制自动化脚本
2、提供同步和异步 API 调用
3、能够集成 Pytest 框架
4、以交互模式运行
简略应用
我的项目地址
https://github.com/microsoft/…
1、下载安装,执行以下命令,别离对应下载 playwright 的库,以及 Chromium、Firefox、WebKit 浏览器的二进制驱动文件:
pip install playwright
python -m playwright install
2、自动化脚本录制:
python -m playwright codegen
脚本录制演示成果:
3、同步 API 调用:
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('http://whatsmyuseragent.org/')
page.screenshot(path=f'example-{browser_type.name}.png')
browser.close()
4、异步 API 调用
import asyncio
from playwright 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.newPage()
await page.goto('http://whatsmyuseragent.org/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
5、集成 Pytest 框架:
def test_playwright_is_visible_on_google(page):
page.goto("https://www.google.com")
page.type("input[name=q]", "Playwright GitHub")
page.click("input[type=submit]")
page.waitForSelector("text=microsoft/Playwright")
6、交互模式运行,交互模式以命令的形式管制浏览器进行相应的操作:
>>> from playwright import sync_playwright
>>> playwright = sync_playwright().start()
# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to see the browser UI
>>> browser = playwright.chromium.launch()
>>> page = browser.newPage()
>>> page.goto("http://whatsmyuseragent.org/")
>>> page.screenshot(path="example.png")
>>> browser.close()
>>> playwright.stop()
7、地位挪动。上面这段代码示意在 iPhone 11 Pro 手机上关上浏览器,关上谷歌地图,定位到某个经纬度(应用了同步 API,异步 API 详见官网,以下相似,不再赘述):
from playwright import sync_playwright
with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.newContext(
**iphone_11,
locale='en-US',
geolocation={'longitude': 12.492507, 'latitude': 41.889938},
permissions=['geolocation']
)
page = context.newPage()
page.goto('https://maps.google.com')
page.click('text="Your location"')
page.screenshot(path='colosseum-iphone.png')
browser.close()
8、在浏览器中执行 JS 脚本。上面这段代码示意关上某个网站,并获取浏览器窗口长度和宽度,并打印进去:
from playwright import sync_playwright
with sync_playwright() as p:
browser = p.firefox.launch()
page = browser.newPage()
page.goto('https://www.example.com/')
dimensions = page.evaluate('''() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
}
}''')
print(dimensions)
browser.close()
9、拦挡网络申请。上面这段代码为浏览器设置了申请路由,拦挡所有的申请并打印申请地址:
from playwright import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.newPage()
def log_and_continue_request(route, request):
print(request.url)
route.continue_()
# Log and continue all network requests
page.route('**', lambda route, request: log_and_continue_request(route, request))
page.goto('http://todomvc.com')
browser.close()
小结
Playwright-python 目前仍不是 v1.0 版本,后续可能会有一些大的 API 变动(可能性不大),并且目前 Playwright-python 的文档都是 JavaScript 版本的文档,API 简直雷同,用起来问题不大,后续会逐步欠缺更新 Python 版本的文档。
开源前哨
日常分享热门、乏味和实用的开源我的项目。参加保护 10 万 + Star 的开源技术资源库,包含:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。