感谢您抽出
阅读本文
一.Playwright简介
Playwright
是微软开源的新一代自动化测试工具。类似于主流的Selenium
。
Playwright
为现代Web
应用程序提供可靠的端到端的自动化测试。
任何浏览器 •任何平台 • 一个API跨浏览器。
Playwright
支持所有现代渲染引擎,包括 Chromium
、WebKit
和Firefox
。
跨平台。在Windows
、Linux
和macOS
上进行本地或CI
、无头或有头测试。
跨语言。在TypeScript
、JavaScript
、Python
、.NET
、Java
中使用Playwright API
。
测试移动网络。适用于Android
和Mobile Safari
的Google Chrome
原生移动仿真。相同的渲染引擎适用于您的桌面和云端。
二.PlayWright安装
官方文档:https://playwright.dev/docs/intro
1.安装方式一
pip install pytest-playwright
2.安装方式二
若安装方式一无法安装(安装过程下载慢,或超时等异常),则采用安装方式二
pip install --index-url http://mirrors.aliyun.com/pypi/simple pytest-playwright --trusted-host mirrors.aliyun.com
3.安装默认浏览器
Playwright
默认下载Chromium
、Firefox
和WebKit
浏览器。
playwright install
Windows下载浏览器
4.安装指定浏览器
playwright install firefox
5.浏览器安装目录
Mac浏览器安装目录:/Users/${user}/Library/Caches/ms-playwright/
三.启动和关闭浏览器
1.启动Chrome
browser = playwright.chromium.launch(headless=False,channel="chrome")
2.关闭浏览器
browser.close()
四.举个例子
1.code demo
import re
import time
from playwright.sync_api import Page,expect
# pycharm终端执行pytest --headed
# playwright默认是以无头模式运行的
def test_first_playwright(page: Page):
# 打开浏览器并跳转到百度首页
page.goto("https://www.baidu.com/")
# 预期结果是否包含标题:百度一下
expect(page).to_have_title(re.compile("百度一下"))
# 元素定位text为新闻
get_started = page.locator("text=新闻")
# 点击跳转到新闻页
get_started.click()
time.sleep(5)
2.效果图
五.同步和异步
Playwright
支持API
的两种编写模式:同步和异步。如果你的项目使用asyncio
,你应该使用async API
,否则使用sync API
。
默认情况下,Playwright
以非GUI模式运行浏览器。要查看浏览器UI
,请在启动浏览器时传参数headless=False
。还可以使用slow_mo
来减慢执行速度,如slow_mo=50
。
1.同步模式
import time
import pytest
from playwright.sync_api import sync_playwright
"""
使用同步模式打开博客园,并断言标题
"""
def test_sync_playwright():
with sync_playwright() as p:
# headless=False表示使用GUI模式运行
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto("")
time.sleep(5)
print(page.title())
// 截图并保存在当前目录下
page.screenshot(path="cnblogs_ishot.png")
browser.close()
if __name__ == '__main__':
pytest.main("-v","test_sync_playwright.py")
2.异步模式
import asyncio
import time
from playwright.async_api import async_playwright
"""
1.使用异步模式分别打开chromium/firefox/webkit浏览器
2.浏览器输入
3.截图保存在ishot目录下
"""
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('')
await page.screenshot(path=f'ishot/screenshot-{browser_type.name}.png')
time.sleep(5)
print(await page.title())
await browser.close()
asyncio.run(main())
,时长00:30
六.版本说明
目前支持的浏览器版本如下
- Chromium 105.0.5195.19
- Mozilla Firefox 103.0
- WebKit 16.0
- Google Chrome 104
- Microsoft Edge 104
七.自动等待
Playwright
在执行操作之前对元素执行一系列可操作性检查,以确保这些操作按预期运行。它会自动等待所有相关检查通过,然后才执行请求的操作。如果所需的检查未在给定范围内通过timeout
,则操作失败并显示TimeoutError
。检查列表如下:
八.断言
Playwright
提供了断言以及方便的方法来创建断言,这些断言将等待并重试,直到满足预期的条件。默认情况下,断言超时设置为5
秒。
expect(page.locator(".status")).to_have_text("Submitted")
- expect(locator).not_to_be_checked(**kwargs)
- expect(locator).not_to_be_disabled(**kwargs)
- expect(locator).not_to_be_editable(**kwargs)
- expect(locator).not_to_be_empty(**kwargs)
- expect(locator).not_to_be_enabled(**kwargs)
- expect(locator).not_to_be_focused(**kwargs)
- expect(locator).not_to_be_hidden(**kwargs)
- expect(locator).not_to_be_visible(**kwargs)
- expect(locator).not_to_contain_text(expected, **kwargs)
- expect(locator).not_to_have_attribute(name, value, **kwargs)
- expect(locator).not_to_have_class(expected, **kwargs)
- expect(locator).not_to_have_count(count, **kwargs)
- expect(locator).not_to_have_css(name, value, **kwargs)
- expect(locator).not_to_have_id(id, **kwargs)
- expect(locator).not_to_have_js_property(name, value, **kwargs)
- expect(locator).not_to_have_text(expected, **kwargs)
- expect(locator).not_to_have_value(value, **kwargs)
- expect(locator).not_to_have_values(values, **kwargs)
- expect(locator).to_be_checked(**kwargs)
- expect(locator).to_be_disabled(**kwargs)
- expect(locator).to_be_editable(**kwargs)
- expect(locator).to_be_empty(**kwargs)
- expect(locator).to_be_enabled(**kwargs)
- expect(locator).to_be_focused(**kwargs)
- expect(locator).to_be_hidden(**kwargs)
- expect(locator).to_be_visible(**kwargs)
- expect(locator).to_contain_text(expected, **kwargs)
- expect(locator).to_have_attribute(name, value, **kwargs)
- expect(locator).to_have_class(expected, **kwargs)
- expect(locator).to_have_count(count, **kwargs)
- expect(locator).to_have_css(name, value, **kwargs)
- expect(locator).to_have_id(id, **kwargs)
- expect(locator).to_have_js_property(name, value, **kwargs)
- expect(locator).to_have_text(expected, **kwargs)
- expect(locator).to_have_value(value, **kwargs)
- expect(locator).to_have_values(values, **kwargs)
- expect(page).not_to_have_title(title_or_reg_exp, **kwargs)
- expect(page).not_to_have_url(url_or_reg_exp, **kwargs)
- expect(page).to_have_title(title_or_reg_exp, **kwargs)
- expect(page).to_have_url(url_or_reg_exp, **kwargs)
- expect(api_response).not_to_be_ok()
- expect(api_response).to_be_ok()
由于时间和精力有限,今天就分享到这里,咱下次接着分享