Skip to main content

命令行

介绍

¥Introduction

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

¥Here are the most common options available in the command line.

  • 运行所有测试

    ¥Run all the tests

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

    ¥Run a single test file

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

    ¥Run a set of test files

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

    ¥Run files that have my-spec or my-spec-2 in the file name

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

    ¥Run tests that are in line 42 in my-spec.ts

    npx playwright test my-spec.ts:42
  • 使用标题运行测试

    ¥Run the test with the title

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

    ¥Run tests in headed browsers

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

    ¥Run all the tests against a specific project

    npx playwright test --project=chromium
  • 禁用 parallelization

    ¥Disable parallelization

    npx playwright test --workers=1
  • 选择 reporter

    ¥Choose a reporter

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

    ¥Run in debug mode with Playwright Inspector

    npx playwright test --debug
  • 在交互式 UI 模式下运行测试,并具有内置监视模式(预览)

    ¥Run tests in interactive UI mode, with a built-in watch mode (Preview)

    npx playwright test --ui
  • 请求帮忙

    ¥Ask for help

    npx playwright test --help

参考

¥Reference

配置文件 中提供了完整的 Playwright 测试选项集。以下选项可以传递到命令行并优先于配置文件:

¥Complete set of Playwright Test options is available in the configuration file. Following options can be passed to a command line and take priority over the configuration file:

OptionDescription
Non-option argumentsEach argument is treated as a regular expression matched against the full test file path. Only tests from files matching the pattern will be executed. Special symbols like $ or * should be escaped with \. In many shells/terminals you may need to quote the arguments.
-c <file> or --config <file>Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}". Defaults to playwright.config.ts or playwright.config.js in the current directory.
--debugRun tests with Playwright Inspector. Shortcut for PWDEBUG=1 environment variable and --timeout=0 --max-failures=1 --headed --workers=1 options.
--fail-on-flaky-testsFail if any test is flagged as flaky (default: false).
--forbid-onlyFail if test.only is called (default: false). Useful on CI.
--fully-parallelRun all tests in parallel (default: false).
--global-timeout <timeout>Maximum time this test suite can run in milliseconds (default: unlimited).
-g <grep> or --grep <grep>Only run tests matching this regular expression (default: ".*").
-gv <grep> or --grep-invert <grep>Only run tests that do not match this regular expression.
--headedRun tests in headed browsers (default: headless).
--ignore-snapshotsIgnore screenshot and snapshot expectations.
--last-failedOnly re-run the failures.
--listCollect all the tests and report them, but do not run.
--max-failures <N> or -xStop after the first N failures. Passing -x stops after the first failure.
--no-depsDo not run project dependencies.
--output <dir>Folder for output artifacts (default: "test-results").
--only-changed [ref]Only run test files that have been changed between 'HEAD' and 'ref'. Defaults to running all uncommitted changes. Only supports Git.
--pass-with-no-testsMakes test run succeed even if no tests were found.
--project <project-name...>Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects).
--quietSuppress stdio.
--repeat-each <N>Run each test N times (default: 1).
--reporter <reporter>Reporter to use, comma-separated, can be "dot", "line", "list", or others (default: "list"). You can also pass a path to a custom reporter file.
--retries <retries>Maximum retry count for flaky tests, zero for no retries (default: no retries).
--shard <shard>Shard tests and execute only the selected shard, specified in the form "current/all", 1-based, e.g., "3/5".
--timeout <timeout>Specify test timeout threshold in milliseconds, zero for unlimited (default: 30 seconds).
--trace <mode>Force tracing mode, can be "on", "off", "on-first-retry", "on-all-retries", "retain-on-failure", "retain-on-first-failure".
--tsconfig <path>Path to a single tsconfig applicable to all imported files (default: look up tsconfig for each imported file separately).
--uiRun tests in interactive UI mode.
--ui-host <host>Host to serve UI on; specifying this option opens UI in a browser tab.
--ui-port <port>Port to serve UI on, 0 for any free port; specifying this option opens UI in a browser tab.
-u or --update-snapshots [mode]Update snapshots with actual results. Possible values are "all", "changed", "missing", and "none". Running tests without the flag defaults to "missing"; running tests with the flag but without a value defaults to "changed".
--update-source-method [mode]Update snapshots with actual results. Possible values are "patch" (default), "3way" and "overwrite". "Patch" creates a unified diff file that can be used to update the source code later. "3way" generates merge conflict markers in source code. "Overwrite" overwrites the source code with the new snapshot values.
-j <workers> or --workers <workers>Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%).
-xStop after the first failure.