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 the 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. If not passed, 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-testsFails test runs that contain flaky tests. By default flaky tests count as successes.
--forbid-onlyWhether to disallow test.only. Useful on CI.
--global-timeout <number>Total timeout for the whole test run in milliseconds. By default, there is no global timeout. Learn more about various timeouts.
-g <grep> or --grep <grep>Only run tests matching this regular expression. For example, this will run 'should add to cart' when passed -g "add to cart". The regular expression will be tested against the string that consists of the project name, test file name, test.describe titles if any, test title and all test tags, separated by spaces, e.g. chromium my-test.spec.ts my-suite my-test @smoke. The filter does not apply to the tests from dependency projects, i.e. Playwright will still run all tests from project dependencies.
--grep-invert <grep>Only run tests not matching this regular expression. The opposite of --grep. The filter does not apply to the tests from dependency projects, i.e. Playwright will still run all tests from project dependencies.
--headedRun tests in headed browsers. Useful for debugging.
--ignore-snapshotsWhether to ignore snapshots. Use this when snapshot expectations are known to be different, e.g. running tests on Linux against Windows screenshots.
--last-failedOnly re-run the failures.
--listlist all the tests, but do not run them.
--max-failures <N> or -xStop after the first N test failures. Passing -x stops after the first failure.
--no-depsIgnore the dependencies between projects and behave as if they were not specified.
--output <dir>Directory for artifacts produced by tests, defaults to test-results.
--only-changed [ref]Only run test files that have been changed between working tree and "ref". Defaults to running all uncommitted changes with ref=HEAD. Only supports Git.
--pass-with-no-testsAllows the test suite to pass when no files are found.
--project <name>Only run tests from the specified projects, supports '*' wildcard. Defaults to running all projects defined in the configuration file.
--quietWhether to suppress stdout and stderr from the tests.
--repeat-each <N>Run each test N times, defaults to one.
--reporter <reporter>Choose a reporter: minimalist dot, concise line or detailed list. See reporters for more information. You can also pass a path to a custom reporter file.
--retries <number>The maximum number of retries for flaky tests, defaults to zero (no retries).
--shard <shard>Shard tests and execute only selected shard, specified in the form current/all, 1-based, for example 3/5.
--timeout <number>Maximum timeout in milliseconds for each test, defaults to 30 seconds. Learn more about various timeouts.
--trace <mode>Force tracing mode, can be on, off, on-first-retry, on-all-retries, retain-on-failure
--tsconfig <path>Path to a single tsconfig applicable to all imported files. See tsconfig resolution for more details.
--update-snapshots or -uWhether to update snapshots with actual results instead of comparing them. Use this when snapshot expectations have changed.
--workers <number> or -j <number>The maximum number of concurrent worker processes that run in parallel.