Skip to main content

命令行

以下是命令行中最常用的选项。

  • 运行所有测试

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

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

    npx playwright test tests/todo-page/ tests/landing-page/
  • 运行文件名中包含 my-specmy-spec-2 的文件

    npx playwright test my-spec my-spec-2
  • 运行 my-spec.ts 中第 42 行的文件

    npx playwright test my-spec.ts:42
  • 运行带有标题的测试

    npx playwright test -g "add a todo item"
  • 在有头浏览器中运行测试

    npx playwright test --headed
  • 针对特定项目运行所有测试

    npx playwright test --project=chromium
  • 禁用并行化

    npx playwright test --workers=1
  • 选择一个报告器

    npx playwright test --reporter=dot
  • 使用 Playwright Inspector 在调试模式下运行

    npx playwright test --debug
  • 寻求帮助

    npx playwright test --help

参考

完整的 Playwright Test 选项集可在配置文件中找到。以下选项可以传递给命令行并优先于配置文件:

  • --headed: 在有头浏览器中运行测试。对调试很有用。
  • --browser: 在特定浏览器中运行测试。可用选项有 "chromium""firefox""webkit""all" 以同时在所有三个浏览器中运行测试。
  • --debug: 使用 Playwright Inspector 运行测试。PWDEBUG=1 环境变量和 --timeout=0 --max-failures=1 --headed --workers=1 选项的快捷方式。
  • -c <file>--config <file>: 配置文件。如果未传递,则默认为当前目录中的 playwright.config.tsplaywright.config.js
  • -c <dir>--config <dir>: 包含要运行的测试的目录,无需配置文件。
  • --forbid-only: 是否禁止 test.only。在 CI 上很有用。
  • -g <grep>--grep <grep>: 仅运行与此正则表达式匹配的测试。例如,当传递 -g "add to cart" 时,这将运行 'should add to cart'
  • --grep-invert <grep>: 仅运行匹配此正则表达式的测试。与 --grep 相反。
  • --global-timeout <number>: 整个测试运行的总超时时间(以毫秒为单位)。默认情况下,没有全局超时。了解更多关于各种超时的信息。
  • --list: 列出所有测试,但不运行它们。
  • --max-failures <N>-x: 在第一个 N 个测试失败后停止。传递 -x 在第一次失败后停止。
  • --output <dir>: 测试生成的工件目录,默认为 test-results
  • --project <name>: 仅运行指定项目之一的测试。默认运行配置文件中定义的所有项目。
  • --quiet: 是否抑制测试的 stdout 和 stderr。
  • --repeat-each <N>: 运行每个测试 N 次,默认为一次。
  • --reporter <reporter>: 选择一个报告器:极简的 dot、简洁的 line 或详细的 list。有关更多信息,请参阅报告器
  • --retries <number>: 不稳定测试的最大重试次数,默认为零(不重试)。
  • --shard <shard>: 分片测试并仅执行选定的分片,以 current/all 形式指定,从 1 开始,例如 3/5
  • --timeout <number>: 每个测试的最大超时时间(以毫秒为单位),默认为 30 秒。了解更多关于各种超时的信息。
  • --ignore-snapshots: 是否忽略快照。当已知快照预期不同时使用此选项,例如在 Linux 上针对 Windows 截图运行测试。
  • --update-snapshots-u: 是否使用实际结果更新快照而不是比较它们。当快照预期已更改时使用此选项。
  • --workers <number>-j <number>: 并行运行的最大并发工作进程数。