Skip to main content

安装

介绍

¥Introduction

Playwright 专为满足端到端测试的需求而创建。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或持续集成 (CI),无头或使用原生移动模拟进行有头测试。

¥Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.

Playwright 库 可以用作通用浏览器自动化工具,它提供了一组强大的 API 来自动化同步和异步 Python 的 Web 应用。

¥The Playwright library can be used as a general purpose browser automation tool, providing a powerful set of APIs to automate web applications, for both sync and async Python.

本介绍介绍了 Playwright Pytest 插件,它是编写端到端测试的推荐方法。

¥This introduction describes the Playwright Pytest plugin, which is the recommended way to write end-to-end tests.

你将学习

¥You will learn

安装 Playwright Pytest

¥Installing Playwright Pytest

Playwright 建议使用官方 Playwright Pytest 插件 来编写端到端测试。它提供上下文隔离,可在多种浏览器配置下开箱即用地运行。

¥Playwright recommends using the official Playwright Pytest plugin to write end-to-end tests. It provides context isolation, running it on multiple browser configurations out of the box.

首先安装 Playwright 并运行示例测试以查看其运行情况。

¥Get started by installing Playwright and running the example test to see it in action.

安装 pytest 插件

¥Install the Pytest plugin:

pip install pytest-playwright

安装所需的浏览器:

¥Install the required browsers:

playwright install

添加示例测试

¥Add Example Test

在当前工作目录或子目录中创建一个遵循 test_ 前缀约定的文件,例如 test_example.py,并编写以下代码。确保你的测试名称也遵循 test_ 前缀约定。

¥Create a file that follows the test_ prefix convention, such as test_example.py, inside the current working directory or in a sub-directory with the code below. Make sure your test name also follows the test_ prefix convention.

test_example.py
import re
from playwright.sync_api import Page, expect

def test_has_title(page: Page):
page.goto("https://playwright.nodejs.cn/")

# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))

def test_get_started_link(page: Page):
page.goto("https://playwright.nodejs.cn/")

# Click the get started link.
page.get_by_role("link", name="Get started").click()

# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()

运行示例测试

¥Running the Example Test

默认情况下,测试将在 Chromium 上运行。这可以通过 CLI 选项 进行配置。测试以无头模式运行,这意味着运行测试时不会打开任何浏览器 UI。测试结果和测试日志将显示在终端中。

¥By default tests will be run on chromium. This can be configured via the CLI options. Tests are run in headless mode meaning no browser UI will open up when running the tests. Results of the tests and test logs will be shown in the terminal.

pytest

更新 Playwright

¥Updating Playwright

要将 Playwright 更新到最新版本,请运行以下命令:

¥To update Playwright to the latest version run the following command:

pip install pytest-playwright playwright -U

系统要求

¥System requirements

  • Python 3.8 或更高版本。

    ¥Python 3.8 or higher.

  • Windows 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 (WSL)。

    ¥Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).

  • macOS 14 Ventura 或更高版本。

    ¥macOS 14 Ventura, or later.

  • Debian 12、Ubuntu 22.04、Ubuntu 24.04,基于 x86-64 和 arm64 架构。

    ¥Debian 12, Ubuntu 22.04, Ubuntu 24.04, on x86-64 and arm64 architecture.

下一步是什么

¥What's next