# Playwright

For your Playwright suite, [Variance Authority](https://variance-authority.dev/docs/index.md) records which lines
of your application each test's pages ran. You wrap `playwright.config.ts`
once and build the application with probes in it. Then CI runs only the spec
files your change reached, you can read what one test's pages did and ask which
tests cover a line, and visual review runs on your machine and in CI with no
hosted service.

## Try it

```bash
npm install --save-dev @variance-authority/playwright-test @variance-authority/sense @variance-authority/cli
```

```ts
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
import { withTestSelection } from '@variance-authority/playwright-test';

export default defineConfig(withTestSelection(
  { projects: [{ name: 'chromium', use: devices['Desktop Chrome'] }] },
  { preconditions: ['tsconfig.json'] },
));
```

```ts
// tests/fixtures.ts: your specs import test from here
import { test as base, expect } from '@playwright/test';
import { varianceFixtures } from '@variance-authority/playwright-test';

export const test = base.extend(varianceFixtures);
export { expect };
```

Your code runs in the page, so the build the tests drive must place probes in
it: add `testSelectionProbes()` from `@variance-authority/sense/journal` to its
Vite plugins, or `testSelectionRule()` to an Rspack or webpack build. Run
`npx playwright test` once to write the record, change a source file, and run
again:

```bash
$ VARIANCE_AUTHORITY_SINCE= npx playwright test
variance-authority: selected 4 of 31
```

## What you get

### Leave out the spec files a change did not reach

The wrap adds the spec files your change did not reach to `testIgnore`, so
Playwright never loads them. A spec whose test failed, or whose page went to
another origin, is recorded incomplete and runs next time. [Leave out the specs
a change did not
reach](https://variance-authority.dev/reference/packages/playwright-test/index.md#leave-out-the-specs-a-change-did-not-reach)
has the details.

### Place shards by recorded time

Under `npx playwright test --shard 3/4`, the wrap's reporter places whole spec
files by the time each took on the last recorded run, in place of Playwright's
split by test. It needs Playwright 1.62 or newer. [Split a test suite across CI
jobs](https://variance-authority.dev/docs/sharding/index.md) gets those times to CI.

### Read one test's story

Name one spec and one test to `npx playwright test -g` with
`VARIANCE_AUTHORITY_STORY=1` set, then run `variance story`.
The story lists the calls of your code the test's pages made, in order. Each
statement of the spec opens what the page did next, and a navigation marks
where the next document starts. [Test stories](https://variance-authority.dev/docs/test-stories/index.md) shows how to
read one and compare two.

### Ask which tests cover a line

`variance covering --file src/cart.ts --line 40` names the Playwright tests
whose pages ran line 40.
[Covering](https://variance-authority.dev/reference/packages/cli/index.md#covering-which-tests-covered-this-line)
lists every way to ask.

### Visual review

Your test already gets the page into the state you want to review, so visual
review is one call in the test body, on the locator you pass. A changed region
is reported with the component that drew it, and `toHaveScreenshot()` stays
where you use it. [Add one observation to a Playwright test](https://variance-authority.dev/start/playwright/index.md)
sets it up, with no wrap needed.

## Not yet

- Skipping tests inside a kept spec file: Playwright runs it whole.
- Selection in watch mode, UI mode or `--list`.
- Shard placement below Playwright 1.62: the wrap refuses the sharded run.

## Go further

- [Record what a driven page executed](https://variance-authority.dev/reference/packages/sense/index.md#record-what-a-driven-page-executed), and [a build Rspack or webpack bundles](https://variance-authority.dev/reference/packages/sense/index.md#a-build-rspack-or-webpack-bundles).
- [Record what each spec executed](https://variance-authority.dev/reference/packages/playwright-test/index.md#record-what-each-spec-executed): every option of the wrap.
- [Recording what a service executed](https://variance-authority.dev/reference/packages/playwright-test/index.md#recording-what-a-service-executed), for code a server runs.
- [Choose where pixels are made](https://variance-authority.dev/reference/packages/playwright-test/index.md#choose-where-pixels-are-made), deferred or in place.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Playwright beside every other runner.
