命令行
以下是命令行中最常用的选项。
运行所有测试
npx playwright test运行单个测试文件
npx playwright test tests/todo-page.spec.ts运行一组测试文件
npx playwright test tests/todo-page/ tests/landing-page/运行文件名中包含
my-spec或my-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.ts或playwright.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>: 并行运行的最大并发工作进程数。