Skip to main content

运行测试

您可以运行单个测试、一组测试或所有测试。测试可以在一个浏览器或多个浏览器上运行。默认情况下,测试以无头模式运行,这意味着在运行测试时不会打开浏览器窗口,结果将在终端中看到。

你将学到

note

为了获得更好的调试体验,请查看 Playwright 的 VS Code 扩展,您可以在 VS Code 编辑器中直接运行测试、添加断点和调试测试。

命令行

  • 运行所有测试

    npx playwright test
  • 运行单个测试文件

    npx playwright test landing-page.spec.ts
  • 运行一组测试文件

    npx playwright test tests/todo-page/ tests/landing-page/
  • 运行文件名中包含 landinglogin 的文件

    npx playwright test landing login
  • 运行指定标题的测试

    npx playwright test -g "add a todo item"
  • 在有头模式下运行测试

    npx playwright test landing-page.spec.ts --headed
  • 在特定项目上运行测试

    npx playwright test landing-page.ts --project=chromium

调试测试

由于 Playwright 在 Node.js 中运行,您可以使用您选择的调试器进行调试,例如使用 console.log,或者在您的 IDE 中,或者直接在 VS Code 中使用 VS Code 扩展。Playwright 附带了 Playwright Inspector,允许您逐步执行 Playwright API 调用,查看其调试日志并探索 选择器

  • 调试所有测试:

    npx playwright test --debug
  • 调试一个测试文件:

    npx playwright test example.spec.ts --debug
  • 从定义 test(.. 的行号开始调试测试:

    npx playwright test example.spec.ts:42 --debug
Debugging Tests with the Playwright inspector

查看我们关于 调试 的指南,了解更多关于 Playwright Inspector 以及使用 浏览器开发者工具 进行调试的信息。

测试报告

HTML 报告器 向您展示完整的测试报告,允许您按浏览器、通过的测试、失败的测试、跳过的测试和不稳定的测试过滤报告。默认情况下,如果某些测试失败,HTML 报告会自动打开。

npx playwright show-report
HTML Report > Test Reports view

您可以点击每个测试,探索测试的错误以及测试的每个步骤。

HTML Reporter > Test Reports detailed view

接下来