Playwright 是微软开源的端到端(end-to-end)测试框架,可用于现代 Web 应用。Playwright 提供如下特性:
任意浏览器、任意平台、一种 API
弹性、没有古怪的测试
无需折中、无限制:浏览器在不同进程中运行属于不同源的 Web 内容。Playwright 与现代浏览器架构保持一致,在进程外运行测试。这使得 Playwright 摆脱典型的进程内测试运行器限制。
完全隔离、快速执行:
强大的工具:
xxxxxxxxxx
mkdir playwright-demo
cd playwright-demo/
python3 -m venv venv
# 安装 Pytest 插件
venv/bin/pip3 install pytest-playwright
# 安装需要的浏览器
venv/bin/playwright install
在当前工作目录或子目录内部,创建 test_my_application.py
文件,其内容如下:
xxxxxxxxxx
import re
from playwright.sync_api import Page, expect
def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page):
page.goto("https://playwright.dev/")
# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))
# create a locator
get_started = page.locator("text=Get started")
# Expect an attribute "to be strictly equal" to the value.
expect(get_started).to_have_attribute("href", "/docs/intro")
# Click the get started link.
get_started.click()
# Expects the URL to contain intro.
expect(page).to_have_url(re.compile(".*intro"))
在默认情况下,测试运行在 Chromium 上,可通过 CLI 选项进行配置,测试以 Headless 模式运行。测试结果和测试日志被展示在终端中。
xxxxxxxxxx
venv/bin/pytest
Playwright 断言(assertion)是专门为动态网页创建的。检查将自动重试,直到满足必要的条件。Playwright 自带 auto-wait,这意味着它在执行操作前,等待到元素变为可操作的(actionable)。Playwright 提供 expect 函数来编写断言。
下面是展示如何编写使用断言、定位器(locator)和选择器(selector)的测试的示例。
xxxxxxxxxx
import re
from playwright.sync_api import Page, expect
def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page):
page.goto("https://playwright.dev/")
# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))
# create a locator
get_started = page.locator("text=Get started")
# Expect an attribute "to be strictly equal" to the value.
expect(get_started).to_have_attribute("href", "/docs/intro")
# Click the get started link.
get_started.click()
# Expects the URL to contain intro.
expect(page).to_have_url(re.compile(".*intro"))
Playwright 提供 expect
函数,它将一直等待,直到满足预期条件。
xxxxxxxxxx
import re
from playwright.sync_api import expect
expect(page).to_have_title(re.compile("Playwright"))
定位器(Locator)是 Playwright 的自动等待和重试能力的核心部分。定位器是一种随时在网页上查找元素的方法,用于在元素上执行诸如 .click
、.fill
之类的操作。可以使用 page.locator(selector, **kwargs) 方法创建自定义定位器。
xxxxxxxxxx
from playwright.sync_api import expect
get_started = page.locator("text=Get started")
expect(get_started).to_have_attribute("href", "/docs/installation")
get_started.click()
选择器(Selector)是用于创建定位器的字符串。Playwright 支持许多不同的选择器,比如 Text、CSS、XPath 等。阅读 in-depth guide 文档,了解更多关于可用的选择器以及如何进行选择的信息。
xxxxxxxxxx
from playwright.sync_api import expect
expect(page.locator("text=Installation")).to_be_visible()
Playwright Pytest 插件基于 test fixture(比如 built in page fixture)的概念,它将被传给你的测试。由于浏览器上下文,在测试之间,页面(Page)彼此隔离,这相当于开启新的浏览器行为,每个测试获得新环境,即使在一个浏览器中运行多个测试时,也是如此。
xxxxxxxxxx
from playwright.sync_api import Page
def test_basic_test(page: Page):
# ...
可以使用各种各样的 fixture 在测试之前或之后执行代码,以及在它们之间共享对象。函数(function
)作用域的 fixture 具有 beforeEach/afterEach 一样的自动使用行为。模块(module
)作用域的 fixture 具有 beforeAll/afterAll 一样的自动使用行为,它在所有测试之前和所有测试之后运行。
xxxxxxxxxx
import pytest
from playwright.sync_api import Page, expect
fixture(scope="function", autouse=True) .
def before_each_after_each(page: Page):
print("beforeEach")
# Go to the starting url before each test.
page.goto("https://playwright.dev/")
yield
print("afterEach")
def test_main_navigation(page: Page):
# Assertions use the expect API.
expect(page).to_have_url("https://playwright.dev/")
可以运行单个测试、一组测试或全部测试。测试可以运行在一种或多种浏览器上。默认情况下,以 Headless 方式运行测试,这意味着在运行测试时,不会打开浏览器窗口,可以在终端中看到结果。通过使用 --headed
标记,可以以 headed 模式运行测试。
在 Chromium 上运行测试
xxxxxxxxxx
pytest
运行单个测试文件
xxxxxxxxxx
pytest test_login.py
运行一组测试文件
xxxxxxxxxx
pytest tests/todo-page/ tests/landing-page/
使用函数名运行测试