微软新开源!不用写 1 行代码的自动化测试工具
共 3961字,需浏览 8分钟
·
2020-12-20 06:09
【导语】: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/playwright-python
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()
"http://whatsmyuseragent.org/") > page.goto(
"example.png") > page.screenshot(path=
> 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版本的文档。
- EOF -
更多优秀开源项目(点击下方图片可跳转)
如果觉得本文介绍的开源项目不错,欢迎转发推荐给更多人。
分享、点赞和在看
支持我们分享更多优秀开源项目,谢谢!