这是用户在 2024-3-21 21:08 为 https://playwright.dev/docs/intro 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skip to main content
跳转到主要内容

Installation

Introduction 介绍

Playwright Test 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 of Google Chrome for Android and Mobile Safari.
Playwright Test 是专门创建来满足端到端测试需求的。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或在 CI 上,无头或有头,并原生模拟 Google Chrome for Android 和 Mobile Safari。

You will learn 您将学习

Installing Playwright 安装剧作家

Get started by installing Playwright using npm or yarn. Alternatively you can also get started and run your tests using the VS Code Extension.
通过使用 npm 或 yarn 安装 Playwright 开始。或者,您还可以使用 VS Code 扩展开始并运行测试。

npm init playwright@latest

Run the install command and select the following to get started:
运行install命令并选择以下内容开始:

  • Choose between TypeScript or JavaScript (default is TypeScript)
    选择TypeScript或JavaScript(默认为TypeScript)
  • Name of your Tests folder (default is tests or e2e if you already have a tests folder in your project)
    Tests文件夹的名称(如果项目中已经有一个tests文件夹,则默认为tests或e2e)
  • Add a GitHub Actions workflow to easily run tests on CI
    添加GitHub操作工作流以轻松在CI上运行测试
  • Install Playwright browsers (default is true)
    安装Playwright浏览器(默认为true)

What's Installed 已安装的内容

Playwright will download the browsers needed as well as create the following files.
Playwright将下载所需的浏览器并创建以下文件。

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

The playwright.config is where you can add configuration for Playwright including modifying which browsers you would like to run Playwright on. If you are running tests inside an already existing project then dependencies will be added directly to your package.json.
你可以在playwright.config中添加配置,包括修改你想要运行Playwright的浏览器。如果你在一个已经存在的项目中运行测试,那么依赖项将直接添加到你的 package.json

The tests folder contains a basic example test to help you get started with testing. For a more detailed example check out the tests-examples folder which contains tests written to test a todo app.
tests 文件夹包含一个基本的示例测试,以帮助您开始测试。有关更详细的示例,请查看 tests-examples 文件夹,其中包含用于测试todo应用程序的测试。

Running the Example Test
运行示例测试

By default tests will be run on all 3 browsers, chromium, firefox and webkit using 3 workers. This can be configured in the playwright.config file. Tests are run in headless mode meaning no browser will open up when running the tests. Results of the tests and test logs will be shown in the terminal.
默认情况下,测试将运行在所有3个浏览器,铬,火狐和webkit使用3个工人。这可以在playwright.config文件中配置。测试以无头模式运行,这意味着在运行测试时没有浏览器会打开。测试结果和测试日志将显示在终端中。

npx playwright test

tests running in command line See our doc on Running Tests to learn more about running tests in headed mode, running multiple tests, running specific tests etc.
请参阅我们的运行测试文档,以了解更多关于以headed模式运行测试,运行多个测试,运行特定测试等的信息。

HTML Test Reports HTML测试报告

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.
测试完成后,将生成一个HTML Reporter,它向您显示测试的完整报告,允许您通过浏览器筛选报告,通过测试,失败测试,跳过测试和重新测试。您可以单击每个测试并查看测试的错误以及测试的每个步骤。默认情况下,如果某些测试失败,HTML报告将自动打开。

npx playwright show-report

HTML Report

Running the Example Test in UI Mode
在UI模式下运行示例测试

Run your tests with UI Mode for a better developer experience with time travel debugging, watch mode and more.
使用UI模式运行测试,通过时间旅行调试、监视模式等获得更好的开发人员体验。

npx playwright test --ui

UI Mode

Check out or detailed guide on UI Mode to learn more about its features.
查看UI模式或详细指南,了解有关其功能的更多信息。

Updating Playwright 更新剧作家

To update Playwright to the latest version run the following command:
要将Playwright更新到最新版本,请运行以下命令:

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

You can always check which version of Playwright you have by running the following command:
你可以通过运行以下命令来检查你拥有的Playwright版本:

npx playwright --version

System requirements 系统要求

  • Node.js 18+
  • Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).
    Windows 10+、Windows Server 2016+或Windows子系统Linux(WSL)。
  • MacOS 12 Monterey, MacOS 13 Ventura, or MacOS 14 Sonoma.
    MacOS 12 Monterey、MacOS 13 Ventura或MacOS 14索诺马。
  • Debian 11, Debian 12, Ubuntu 20.04 or Ubuntu 22.04, with x86-64 or arm64 architecture.
    Debian 11、Debian 12、Ubuntu 20.04或Ubuntu 22.04,x86-64或arm 64架构。

What's next 下一步是什么