跳至主要内容

安装

简介

Playwright Test是专为满足端到端测试需求而创建的。Playwright支持所有现代渲染引擎,包括Chromium、WebKit和Firefox。可在Windows、Linux和macOS上进行测试,无论是在本地还是CI环境,无头模式或有头模式,并支持Android版Google Chrome和Mobile Safari的原生移动设备模拟。

你将学习

安装Playwright

开始使用Playwright,可以通过npm、yarn或pnpm进行安装。另外,您也可以使用VS Code扩展来开始并运行您的测试。

npm init playwright@latest

运行安装命令并选择以下选项以开始使用:

  • 在TypeScript或JavaScript之间选择(默认为TypeScript)
  • 测试文件夹的名称(默认是tests,如果项目中已存在tests文件夹则为e2e)
  • 添加一个GitHub Actions工作流,以便在CI上轻松运行测试
  • 安装Playwright浏览器(默认为true)

已安装内容

Playwright将下载所需的浏览器并创建以下文件。

playwright.config.ts
package.json
package-lock.json
tests/
example.spec.ts
tests-examples/
demo-todo-app.spec.ts

playwright.config 是您可以添加Playwright配置的地方,包括修改您希望在哪些浏览器上运行Playwright。如果您在已有项目中运行测试,依赖项将直接添加到您的package.json中。

tests 文件夹包含一个基础示例测试,帮助您开始测试。如需更详细的示例,请查看 tests-examples 文件夹,其中包含为测试待办事项应用而编写的测试。

运行示例测试

默认情况下,测试将在所有3种浏览器上运行,包括Chromium、Firefox和WebKit,使用3个工作线程。这可以在playwright.config文件中进行配置。测试以无头模式运行,意味着执行测试时不会打开浏览器窗口。测试结果和测试日志将显示在终端中。

npx playwright test

tests running in command line 查看我们的运行测试文档,了解更多关于在headed模式下运行测试、运行多个测试、运行特定测试等内容。

HTML Test Reports

After your test completes, an HTML Reporter will be generated, which shows you a full report of your tests allowing you to filter the report by browsers, passed tests, failed tests, skipped tests and flaky tests. You can click on each test and explore the test's errors as well as each step of the test. By default, the HTML report is opened automatically if some of the tests failed.

npx playwright show-report

HTML Report

在UI模式下运行示例测试

使用UI模式运行您的测试,以获得更好的开发者体验,包括时间旅行调试、监视模式等功能。

npx playwright test --ui

UI Mode

查看我们的UI模式详细指南以了解更多关于其功能的信息。

更新Playwright

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

npm install -D @playwright/test@latest
# Also download new browser binaries and their dependencies:
npx playwright install --with-deps

您可以通过运行以下命令随时检查您安装的Playwright版本:

npx playwright --version

系统要求

  • 最新版本的 Node.js 18、20 或 22。
  • Windows 10及以上版本、Windows Server 2016及以上版本或适用于Linux的Windows子系统(WSL)。
  • macOS 13 Ventura 或更高版本。
  • Debian 12、Ubuntu 22.04、Ubuntu 24.04,支持x86-64和arm64架构。

下一步