Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

playwright-test

hugomrdias25.7kMIT14.1.7TypeScript support: included

Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with playwright.

playwright, test, tests, test-runner, mocha, tape, benchmark, benchmark.js, zora, coverage, istanbul, nyc, code coverage, uvu

readme

playwright-test NPM Version NPM Downloads NPM License tests

Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with playwright.

Install

pnpm install playwright-test

Usage

playwright-test [files] [options]
# or
pw-test [files] [options]

Options

Description
    Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with `playwright` and in Node.

  Usage
    $ playwright-test [files] [options]

  Options
    -r, --runner       Test runner. Options: mocha, tape, zora, uvu, none, taps and benchmark. Internal runners are autodetected by default. It also accepts a path to a module or a module ID that exports a `playwrightTestRunner` object.
    -b, --browser      Browser to run tests. Options: chromium, firefox, webkit.  (default chromium)
    -m, --mode         Run mode. Options: main, worker and node.  (default main)
    -d, --debug        Debug mode, keeps browser window open.  (default false)
    -w, --watch        Watch files for changes and re-run tests.
    -i, --incognito    Use incognito window to run tests.  (default false)
    -e, --extension    Use extension background_page to run tests.  (default false)
    --cov              Enable code coverage in istanbul format. Outputs '.nyc_output/coverage-pw.json'.  (default false)
    --report-dir       Where to output code coverage in instanbul format.  (default .nyc_output)
    --before           Path to a script to be loaded on a separate tab before the main script.
    --sw               Path to a script to be loaded in a service worker.
    --assets           Folder with assets to be served by the http server.  (default process.cwd())
    --cwd              Current directory.  (default /Users/hd/code/playwright-test)
    --extensions       File extensions allowed in the bundle.  (default js,cjs,mjs,ts,tsx)
    --config           Path to the config file
    -v, --version      Displays current version
    -h, --help         Displays this message


  Examples
    $ playwright-test test.js --runner tape
    $ playwright-test test --debug
    $ playwright-test "test/**/*.spec.js" --browser webkit --mode worker --incognito --debug

    $ playwright-test bench.js --runner benchmark
    # Uses benchmark.js to run your benchmark see playwright-test/mocks/benchmark.js for an example.

    $ playwright-test test --cov && npx nyc report --reporter=html
    # Enable code coverage in istanbul format which can be used by nyc.

    $ playwright-test "test/**/*.spec.js" --debug --before ./mocks/before.js
    # Run a script in a separate tab. Check ./mocks/before.js for an example.
    # Important: You need to call `self.PW_TEST.beforeEnd()` to start the main script.

  Runner Options
    All arguments passed to the cli not listed above will be fowarded to the runner.
    $ playwright-test test.js --runner mocha --bail --grep 'should fail'

    To send a `false` flag use --no-bail.
    Check https://mochajs.org/api/mocha for `mocha` options or `npx mocha --help`.

  Notes
    DEBUG env var filtering for 'debug' package logging will work as expected.
    $ DEBUG:app playwright-test test.js

    Do not let your shell expand globs, always wrap them.
    $ playwright-test "test/**" GOOD
    $ playwright-test test/** BAD

Client

This client package exposes the playwright-test options and some Playwright browser context methods to be used in tests.

import * as Client from 'playwright-test/client'

it('should setoffline', async () => {
  if (Client.mode === 'main' && Client.options.extension === false) {
    globalThis.addEventListener('offline', () => {
      console.log('offlineee')
    })
    await Client.context.setOffline(true)
    equal(navigator.onLine, false)
    await Client.context.setOffline(false)
    equal(navigator.onLine, true)
  }
})

it('should geolocation', async () => {
  if (Client.mode === 'main') {
    const deferred = pdefer()
    await Client.context.setGeolocation({
      latitude: 59.95,
      longitude: 30.316_67,
    })
    await Client.context.grantPermissions(['geolocation'])

    navigator.geolocation.getCurrentPosition((position) => {
      deferred.resolve(position)
    })

    const position = (await deferred.promise) as GeolocationPosition
    equal(position.coords.latitude, 59.95)
  }
})

Flow control

All test runners support automatic flow control, which means you don't need to call special function or trigger any event in your tests to stop the run. The none runner does not support flow control.

To manually stop the run you can use process.exit:

process.exit(0) // stops the run and exits with success
process.exit(1) // stops the run and exits with failure

Custom test runner

You can define a custom test runner by passing a path to a file or a node module id that exports an object called playwrightTestRunner that implements the TestRunner interface.


$ playwright-test test.js --runner ./my-runner.js
# or
$ playwright-test test.js --runner my-runner

You can also just define you test runner in the config file.

// playwright-test.config.js
/** @type {import('../src/runner.js').RunnerOptions} */
const config = {
  testRunner: {
    compileRuntime: (options, paths) => {
      return `
import mocha from 'mocha/mocha.js'
mocha.setup({
    reporter: 'spec',
    timeout: 5000,
    ui: 'bdd',
})

${paths.map((url) => `await import('${url}')`).join('\n')}

  mocha
    .run((f) =>{
      process.exit(f)
    })
        `
    },
  },
}

export default config
export interface TestRunner {
  /**
   * Module ID name used to import the test runner runtime.
   * Used in auto detection of the test runner.
   */
  moduleId: string
  /**
   * Options made available to the compiled runtime.
   * This is useful to pass options to the test runner.
   *
   * @example
   * ```js
   * const options = JSON.parse(process.env.PW_OPTIONS)
   * const testRunnerOptions = options.testRunner.options
   * ```
   */
  options?: unknown
  /**
   * Esbuild config for the test runner
   */
  buildConfig?: BuildOptions
  /**
   * Compile runtime entry point for esbuild
   *
   * @param options - Runner options
   * @param testPaths - Test paths
   * @returns
   */
  compileRuntime: (options: RunnerOptions, testPaths: string[]) => string
}

Config

The config file needs to be commonjs for now, so if your package is pure ESM you need to use .cjs extension.

Configuration can be done with cli flags or config files.

package.json, // using property `pw-test` or `playwright-test`
.playwright-testrc.json,
.playwright-testrc.js,
.playwright-testrc.cjs,
playwright-test.config.js,
playwright-test.config.cjs,
.pw-testrc.json,
.pw-testrc.js,
.pw-testrc.cjs,
pw-test.config.js,
pw-test.config.cjs,
.config/playwright-testrc.json
.config/playwright-testrc.js
.config/playwright-testrc.cjs
.config/pw-testrc.json
.config/pw-testrc.js
.config/pw-testrc.cjs

The config type can be imported from the entrypoint.

/** @type {import('playwright-test').RunnerOptions} */
const config = {
  // ...
}

export default config

The config file can also export a function that receives the cli options as argument.

/** @type {import('playwright-test').ConfigFn} */
function buildConfig(cliOptions) {
  return {
    buildConfig: {
      bundle: cliOptions.mode !== 'node',
    },
  }
}

export default buildConfig

Interface

export interface RunnerOptions {
  input?: string[]
  testRunner: TestRunner
  cwd: string
  extensions: string
  browser: 'chromium' | 'firefox' | 'webkit'
  debug: boolean
  mode: 'main' | 'worker' | 'node'
  incognito: boolean
  extension: boolean
  assets: string
  before?: string
  sw?: string
  cov: boolean
  reportDir: string
  buildConfig: BuildOptions
  buildSWConfig: BuildOptions
  browserContextOptions?: BrowserContextOptions
  beforeTests: (opts: RunnerOptions) => Promise<unknown>
  afterTests: (
    opts: RunnerOptions,
    beforeTestsOutput: unknown
  ) => Promise<unknown>
}

Run in CI

Check our CI config .github/workflows/main.yml and the playwright Github Action

License

MIT © Hugo Dias

changelog

Changelog

14.1.7 (2024-11-19)

Bug Fixes

14.1.6 (2024-09-10)

Bug Fixes

  • log playwright browser download progress on stderr (#682) (bb6955c)

14.1.5 (2024-09-06)

Bug Fixes

14.1.4 (2024-07-23)

Bug Fixes

14.1.3 (2024-05-07)

Bug Fixes

  • test for skipped browser warnings early (#661) (f7248e9)

14.1.2 (2024-05-02)

Bug Fixes

14.1.1 (2024-02-08)

Bug Fixes

14.1.0 (2024-01-31)

Features

  • update playwright core and dim browser msgs (727cc1e)

14.0.0 (2023-11-23)

⚠ BREAKING CHANGES

  • config function receives cli options and process.env.PW_TEST remove in favor of stringified process.env.PW_OPTIONS

Features

  • config function receives cli options and process.env.PW_TEST remove in favor of stringified process.env.PW_OPTIONS (6d6892f)

Bug Fixes

  • update playwright and lilconfig (0e40e69)

13.0.1 (2023-11-17)

Bug Fixes

  • make skip and only also a suite (d7a5c41)

13.0.0 (2023-11-13)

⚠ BREAKING CHANGES

  • before/after hooks receive the runner env instead of just the options.

Features

  • before/after hooks receive the runner env instead of just the options. (b392f7d)

12.6.1 (2023-11-13)

Bug Fixes

  • avoid nodejs console.timeEnd warning (6cf6f6e)

12.6.0 (2023-11-09)

Features

  • add support for .node files (67300ce)

12.5.0 (2023-11-09)

Features

  • add server url to the client (454abec)

12.4.3 (2023-10-19)

Bug Fixes

  • move client to src and export properly (6238117)

12.4.2 (2023-10-19)

Bug Fixes

12.4.1 (2023-10-19)

Bug Fixes

  • normalize logs and stop node run (4403489)

12.4.0 (2023-10-17)

Features

  • added playwright/client to exposes browser context methods to the tests (7111801), closes #540
  • safer exit and logging (b3710e1)

Bug Fixes

  • skip before and after hooks when no tests (2c5ca4f)

12.3.8 (2023-10-02)

Bug Fixes

  • improve node errors and suite errors (43bc597)

12.3.7 (2023-09-29)

Bug Fixes

  • add NODE_ENV=test, fix autodetect and external msw/node (62fe8e1)

12.3.6 (2023-09-29)

Bug Fixes

  • keep watching even if build fails (b244658)

12.3.5 (2023-09-25)

Bug Fixes

  • deps: bump playwright-core from 1.38.0 to 1.38.1 (#593) (dd79c5a)

12.3.4 (2023-09-18)

Miscellaneous Chores

12.3.3 (2023-09-15)

Bug Fixes

  • fix node mode watch and top level imports (36c8ce2)

12.3.2 (2023-09-15)

Bug Fixes

12.3.1 (2023-09-13)

Bug Fixes

12.3.0 (2023-09-05)

Features

Bug Fixes

  • fix stack formating in and colors in taps (c63eebd)
  • remove assert from callback (4a8c22f)

12.2.0 (2023-09-01)

Features

  • add assert exports to taps (6369dcd)

Bug Fixes

  • fix autodetect for mocha only/skip (d0ae762), closes #572

12.1.2 (2023-08-28)

Bug Fixes

12.1.1 (2023-07-14)

Bug Fixes

  • add wasm loader to node mode (f71280d)

12.1.0 (2023-07-14)

Features

  • add support for wasm loader (f223465)
  • update playwright to 1.36.0 (a59e0a4)

12.0.0 (2023-07-14)

⚠ BREAKING CHANGES

  • process.stdout/stderr without console.log (#563)

Features

  • process.stdout/stderr without console.log (#563) (2f5baf0)

11.0.4 (2023-07-13)

Bug Fixes

  • support ts in autodetect (2d8ea1e)

11.0.3 (2023-07-07)

Bug Fixes

  • auto detect logic for requires (88638bd)
  • runner/module resolution logic (#562) (a0c5be5)

11.0.2 (2023-07-04)

Bug Fixes

11.0.1 (2023-06-30)

Bug Fixes

11.0.0 (2023-06-30)

⚠ BREAKING CHANGES

  • Add support for node, internal runner autodetect, path module ids and taps.

Features

  • Add support for node, internal runner autodetect, path module ids and taps. (1d73a36)

Bug Fixes

10.0.1 (2023-06-29)

Bug Fixes

  • more colors and experiental runner and autodetect (477ec99)

10.0.0 (2023-06-27)

⚠ BREAKING CHANGES

Features

Bug Fixes

Miscellaneous Chores

9.2.0 (2023-06-15)

Miscellaneous Chores

9.1.0 (2023-05-12)

Features

9.0.0 (2023-04-28)

⚠ BREAKING CHANGES

  • tests now run as ESM modules

Features

Bug Fixes

8.4.0 (2023-04-28)

Features

  • extensions use the new headless mode (9d33a74)
  • update deps (afc261c)

Bug Fixes

  • only install deps not browser in ci linux (db46d19)

8.3.0 (2023-04-06)

Features

8.2.0 (2023-01-27)

Features

8.1.2 (2022-12-12)

Bug Fixes

  • deps: bump v8-to-istanbul from 9.0.0 to 9.0.1 (#460) (6862c78)

8.1.1 (2022-06-16)

Bug Fixes

8.1.0 (2022-06-16)

Features

8.0.0 (2022-05-16)

⚠ BREAKING CHANGES

  • update mocha, playwright and others, fix sourcemaps (#435)

Features

  • update mocha, playwright and others, fix sourcemaps (#435) (b6852a4)

7.4.1 (2022-05-16)

Bug Fixes

  • try to remove any races between copy and server start (67be737)

7.4.0 (2022-04-06)

Features

7.3.0 (2022-03-19)

Features

  • update zora, pw and others (0bbf67e)

7.2.2 (2021-11-16)

Bug Fixes

7.2.1 (2021-11-11)

Bug Fixes

7.2.0 (2021-10-31)

Features

Bug Fixes

7.1.2 (2021-10-29)

Bug Fixes

  • make after hook always run (9e45644)

7.1.1 (2021-10-20)

Bug Fixes

7.1.0 (2021-09-09)

Features

7.0.4 (2021-09-06)

Bug Fixes

7.0.3 (2021-08-19)

Bug Fixes

  • deps: bump esbuild from 0.12.15 to 0.12.21 (#279) (5d1a824)

7.0.2 (2021-07-15)

Bug Fixes

7.0.1 (2021-07-13)

Bug Fixes

  • unregister sw in the proper spot (6650f17)

7.0.0 (2021-07-13)

⚠ BREAKING CHANGES

  • shouldn't break anything but a lot changed internally so to be same review your tests!

Features

  • new watcher and better sw (fbfd613)

Bug Fixes

  • cli version read and catch cli action errors (db3709c)
  • remove debug output from internal tools (7f08409)

6.0.0 (2021-07-07)

⚠ BREAKING CHANGES

  • service worker is registered after the tests bundle is requested

Features

5.0.0 (2021-05-17)

⚠ BREAKING CHANGES

  • move to esm (#216)

Features

4.1.0 (2021-05-13)

Features

4.0.0 (2021-05-12)

⚠ BREAKING CHANGES

  • esbuild 0.11.20 and pw 1.11.0

Features

Bug Fixes

3.0.8 (2021-05-12)

Bug Fixes

3.0.7 (2021-04-27)

Bug Fixes

3.0.6 (2021-04-27)

Bug Fixes

3.0.5 (2021-04-26)

Bug Fixes

3.0.4 (2021-04-08)

Bug Fixes

  • print out the name of the browser during setup (#178) (5393493)
  • wait for runner to me in the global scope inside workers (7de86a4)

3.0.3 (2021-04-08)

Bug Fixes

  • deps: bump esbuild from 0.11.5 to 0.11.6 (#176) (46bf3f2)

3.0.2 (2021-04-07)

Bug Fixes

  • dont wait for worker because we will wait for the event after (d62c005)

3.0.1 (2021-04-07)

Bug Fixes

  • do not delete tmp browser dir to avoid race (b56b11c)

3.0.0 (2021-04-06)

⚠ BREAKING CHANGES

  • too many changes and esbuild updated

Bug Fixes

  • fix extensions, cov,formating, types, errors and workers (#168) (9593c3d)
  • move fresh-tape to deps and fix cov (#173) (30cd1dc)