What Playwright.Test records, how to rerun failures, and how the browser pool runs. The
reference is Playwright.Test's Artifacts and
Playwright.Test.BrowserPool. Snippets alias Playwright.{Browser, BrowserContext, Test} and
Playwright.Test.BrowserPool.
What to turn on
For most suites:
# config/test.exs
config :playwright, :test,
trace: :retain_on_failure,
screenshot: :only_on_failure
# test/test_helper.exs
github = if System.get_env("GITHUB_ACTIONS"), do: [Playwright.Test.GitHubFormatter], else: []
ExUnit.start(formatters: [ExUnit.CLIFormatter, Playwright.Test.HTMLFormatter | github])A failed test then keeps its trace, screenshot and error-context.md under test-results/, and
mix playwright.show_report opens the run's report. On CI, upload test-results and
playwright-report. To trace only reruns, which costs less, see Retries.
Traces, screenshots and videos
Every context a test creates on a pooled browser is recorded, also in a Task of the test or a
route handler. Each test writes to a dir of its own under test-results/, named after the test
file and name, as Playwright Test's testInfo.outputDir, and a short hash of the module (and its
parameterize: parameters) that tells same-named tests apart:
test-results/sign_up-signs-up-5d41a/trace.zip
test-results/sign_up-signs-up-5d41a/test-failed-1.pngA failed test also gets Playwright Test's error-context.md: its errors, its source and an aria
snapshot of its page, for you or an agent to read. A failed test that keeps a trace prints how
to open it after the run: mix playwright.cli show-trace test-results/.../trace.zip. More in the
Debugging and CI guide. A trace also shows what the test printed
(Output). With the app in the test VM, the trace and
error-context.md can show the server's side too: The server in the trace.
trace, screenshot and video are :off by default, as in Playwright Test.
:retain_on_failure (trace, video) and :only_on_failure (screenshot) record every test and
keep the files of failures; a passing test's are dropped at its end. They are options of every
level (Options, devices and projects), as a mode:
use Playwright.Case, trace: :retain_on_failure, video: :retain_on_failure
@tag Test.use(trace: :on)
test "checks out", %{page: page} do
# ...
endAs in Playwright Test, they take options too, with the mode as :mode: trace: [mode: :on, snapshots: false], screenshot: [mode: :only_on_failure, full_page: true], video: [mode: :on, size: %{width: 640, height: 480}, fps: 10, show: %{test: %{level: "step"}}] (show.test writes
the test's title and steps over the video). Each replaces the value under it as a whole; an
unknown use option fails the module's compilation, and an unknown mode records nothing, as
upstream's.
Recording slows every test it records, as in Playwright Test. On one suite (Chromium, 10
actions a test, one machine, 1.65), roughly: trace: :on, screenshot: :on added about
220 ms a test. Most of it is the trace's DOM snapshots, taken whenever a test traces:
trace: [mode: :on, snapshots: false], screenshot: :on keeps the actions and the trace's
screenshots (:screenshots) and added about 50 ms. Saving and merging the trace come next,
growing with its size; :retain_on_failure records the same, but on a pass skips the merge, and
the save of the contexts still open at its end. The screenshot is the least, and
:only_on_failure still takes it when the context closes, as upstream's, and drops it on a
pass. Playwright Test's docs warn against trace: :on on CI; they recommend on-first-retry,
see Retries.
Files of your own go in the test's dir with Playwright.Test.output_path("export.csv"); they
stay when the test passes. Playwright.Test.attach("export", path: path) (or body:) also shows
one in the trace, as Playwright Test's testInfo.attach. The files kept are the test's
attachments too, after yours, as Playwright Test's testInfo.attachments: screenshot, video,
error-context and trace, each with its path.
The files are written in the test's on_exit, which ExUnit runs in reverse order of
registration. An on_exit that reads them (to check a trace.zip, say) must therefore be
registered before the recording's: in a setup above use Playwright.Test.
Playwright.Test.info(test_pid) there has the test's result, as Playwright Test's testInfo has
it in teardown (status, errors, duration):
setup %{test_pid: test_pid} do
on_exit(fn ->
%{status: status, attachments: attachments} = Playwright.Test.info(test_pid)
for %{name: "trace", path: path} <- attachments, status != :passed, do: MyApp.CI.upload(path)
end)
end
use Playwright.TestA report of the run
Playwright.Test.HTMLFormatter writes Playwright Test's HTML report to playwright-report/:
each test's errors, steps, attachments and trace, in the browser.
# test/test_helper.exs
ExUnit.start(formatters: [ExUnit.CLIFormatter, Playwright.Test.HTMLFormatter])Open it with mix playwright.show_report (mix playwright.show_report reports/html for another
output_folder). Each run replaces the report, so give a rerun its own folder:
PLAYWRIGHT_HTML_OUTPUT_DIR=playwright-report-retry mix test --failed.
html_report: [open: :on_failure] serves the report after a failed run and waits for Ctrl+C, as
Playwright Test does by default. Options: Playwright.Test.HTMLFormatter.
For a script or a CI summary, Playwright.Test.JSONFormatter writes the same results as JSON, in
the json reporter's shape: each test's result (status, duration, errors, steps) and its
attachments, the paths of its trace, screenshots and videos. Read the paths from there rather
than guessing them from test names:
# test/test_helper.exs
ExUnit.start(formatters: [ExUnit.CLIFormatter, Playwright.Test.JSONFormatter])
# config/test.exs
config :playwright, :test, json_output_file: "test-results/results.json"PLAYWRIGHT_JSON_OUTPUT_FILE overrides the config, as in Playwright Test; without either, the
report goes to standard output, mixed with ExUnit.CLIFormatter's output unless the formatter
runs on its own (mix test --formatter Playwright.Test.JSONFormatter). Its fields, and what
ExUnit can't fill in: Playwright.Test.JSONFormatter.
Partitioned runs
Each partition of mix test --partitions is a run of its own, so its report has only its own
tests. For one report of the whole suite, add Playwright.Test.PartitionFormatter: each
partition writes its results and attachments to partition-report/report-N.zip, where N is
its MIX_TEST_PARTITION:
# test/test_helper.exs
ExUnit.start(formatters: [ExUnit.CLIFormatter, Playwright.Test.PartitionFormatter])Collect the zips in one folder and merge them with mix playwright.merge_reports. It writes
the HTML report (--reporter html, the default), the JSON report (--reporter json) or both,
with the formatters' usual options. Those options live in config/test.exs, so run the task in
the test environment:
MIX_ENV=test mix playwright.merge_reports partition-report --reporter html,json
To leave out MIX_ENV=test, add def cli, do: [preferred_envs: ["playwright.merge_reports": :test]]
to your mix.exs. The zips are extracted next to themselves in that folder, and the JSON report's
attachment paths point there. The partitions and the merge must run in the same checkout path,
as the report reads the test files there.
Start each run with an empty folder: mix test leaves old zips in place. The merge fails when it
can tell the folder holds reports of different runs (a partition there twice, different partition
counts, or a test in two reports), but not for an old zip that happens to fit. A partition without
test files has no report (Mix runs it without formatters): the merge warns and goes on without it.
It knows the partition count from mix test --partitions N on the command line, not when an alias
adds --partitions; then only a test in two reports fails it. Keep the zips out of the HTML
report's output folder, which is removed before the report is written: the merge refuses that. A CI
workflow:
Debugging and CI.
Retries
Playwright Test's answer to what tracing costs (above) is to
trace only the retry of a failure (on-first-retry). ExUnit has no retries in a run, but
mix test --failed is one: under it a test is Playwright Test's first retry
(Playwright.Test.info().retry is 1), and the retry modes go by it. So trace only the rerun:
# config/test.exs
config :playwright, :test, trace: :on_first_retryOn Elixir 1.21 or later, a job without a coverage check can retry on exit status 2 (tests failed):
mix test || { [ $? -eq 2 ] && mix test --failed; }
If the job uses --cover, keep the initial run's status even on Elixir 1.21 or later.
A run can fail both tests and the coverage threshold, and rerunning only failed tests
doesn't recheck the full suite's coverage. Use the diagnostic retry below instead.
Call mix test itself, not through an alias that runs it with mix cmd: mix cmd turns exit
status 2 into 1, so the retry never fires. In CI, with --partitions and annotations:
Annotations.
On Elixir 1.20 and earlier, --failed can miss a failure, and the retry then passes or runs
nothing and exits 0: a test of a parameterize: module (Projects) passing with
another parameter set later in the run erases its failure
(elixir-lang/elixir#15820), as does a
setup_all's on_exit failing after its tests passed
(elixir-lang/elixir@0f654791). Running
one project per mix test avoids the common case, the same test in another project. Failures
across your own parameter lists, and that on_exit failure, can still be lost. In CI, run one
project per job, each the job from GitHub Actions with its
project's browser and an artifact name of its own (test-results-${{ matrix.browser }}):
strategy:
fail-fast: false
matrix:
include:
- {project: "Desktop Chrome", browser: chromium}
- {project: "Desktop Firefox", browser: firefox}
- {project: "Desktop Safari", browser: webkit}
env:
PROJECTS: ${{ matrix.project }}
steps:
# ...
- run: mix playwright.install ${{ matrix.browser }} --with-deps
- run: mix testA rerun that lost every failure prints There are no tests to run and exits 0. Checking for
that message only catches an empty retry. If a setup_all teardown fails and an unrelated test
fails, the retry can run just that test and pass. On affected Elixir versions, or in a coverage
job, keep the initial CI run failed and retry only for diagnostics. In the matrix above,
replace run: mix test with these two steps; both inherit the job's PROJECTS. For a coverage
job, add --cover to the first command only.
- id: tests
run: |
status=0
mix test || status=$?
echo "status=$status" >> "$GITHUB_OUTPUT"
exit "$status"
- if: ${{ failure() && steps.tests.outputs.status == '2' }}
run: mix test --failedThe other retry modes are Playwright Test's too: :on_all_retries traces every rerun,
:retain_on_first_failure the first run's failures, :retain_on_failure_and_retries both;
video takes them as well, and screenshot has :on_first_failure.
Passing runs pay nothing for it, and the first run's files stay. A test's dir is emptied before
it runs, so a test that passes now keeps no error-context.md of an earlier failure. A rerun
writes to <dir>-retry1, next to the first run's dir:
test-results/sign_up-signs-up-5d41a/error-context.md
test-results/sign_up-signs-up-5d41a-retry1/trace.zipA flaky test that passes on the rerun keeps that passing run's trace; for the failure's own too,
:retain_on_failure_and_retries. Which dirs a run deletes, and preserve_output:
Artifacts.
Workers
A worker, Playwright Test's, is a driver and its browser, held by the module (setup_all) or
test that calls BrowserPool.browser/1: reused warm after a passing scope, replaced after a
failing one (Playwright.Test.BrowserPool has the rules). That needs the tests' results, so
use Playwright.Test: without it a module counts as passed. To set up each new driver
(register selectors, say), pass on_start: &MyApp.Selectors.register/1: the driver's state then
holds for every scope that gets it.
The test id attribute of get_by_test_id is global to the VM, as Playwright's is to a worker
process, so a module that sets it changes it for every test running at once. Set it for the
suite, as Playwright Test's testIdAttribute; each pooled driver gets it:
# config/test.exs
config :playwright, :test, test_id_attribute: "data-pw"At most workers drivers run at once (config, default half the schedulers, as Playwright Test's
workers); the others wait, first come first served, inside BrowserPool.browser/1: in
setup_all outside its timeout, in setup or a test within ExUnit's test timeout. A wait in
setup_all costs nothing, so ExUnit's max_cases can stay as it is; one in setup or a test
counts against the test's timeout, and so does a worker it starts or restarts (a driver start and
browser launch: about half a second for Firefox, more on CI), as after a failed test reset it.
Taken in setup_all, as Playwright.Case does, the module's browser starts outside every test's
timeout. A worker is a driver and one browser
of one set of options, as Playwright Test's worker runs one project: a call with other options
(another browser_name, headless, channel, launch_options, connect_options, driver or
on_start) takes another worker, which may
restart an idle one of other options. ExUnit runs modules in random order, so in a run that
mixes options each switch restarts a worker (a driver start, about 110 ms, plus the browser's
launch). Run one configuration per invocation, as Playwright Test's projects effectively do: tag
each module with its configuration (@moduletag browser: :firefox) and run
mix test --only browser:firefox, or give each CI job one. ExUnit runs async: false modules
one at a time, after the async ones, so they never wait on workers: a rare module with unusual
options can be one, which at most restarts an idle worker of the others once they're done. In a
suite of only those, workers only sets how many configurations stay warm.
Or keep each configuration's workers warm, at the cost of memory: with workers above
max_cases (up to max_cases per configuration), no more than max_cases modules hold a worker
at once. A module then finds an idle worker of its options or starts a new one, rather than
restarting another configuration's. LiveView's e2e suite on Chromium, Firefox and WebKit in one
run at max_cases 9 took 80 s with 9 workers, 61.8 s with 18 and 57.5 s with 27, at about
11 GB peak memory.
The defaults: workers half the schedulers (Playwright Test's default, half the cores), and
ExUnit's max_cases unchanged, twice the schedulers. So about four times as many modules run as
Playwright Test's workers: those past workers wait inside browser/1 at no cost, and modules
that never call it are unaffected. workers stays below max_cases because each running
browser test keeps a browser, its renderer and the driver busy: on an 18-core Mac, LiveView's
e2e suite at max_cases 36 took 64.6 s at 15.0 GB peak memory with 9 workers, and 80.2 s at
38.7 GB with 36.
A suite whose tests mostly wait on the app needs more: raise workers toward the core count. An
app's full e2e suite on 18 cores at max_cases 18 took 93.8 s at 6.5 GB with 16 workers,
against 113.5 s at 6.1 GB with the default 9. Keep max_cases at about the core count for
browser suites: past it the app's CPU saturates. At max_cases 36 the same suite gained nothing
(100.9 to 102.7 s) and hit 4 s action timeouts.
Debugging one test
To watch one module run headed, slowed down, give it a browser with those options:
@moduletag :debug
setup_all do
%{browser: BrowserPool.browser(headless: false, launch_options: [slow_mo: 500])}
end
setup %{browser: browser} do
context = Browser.new_context!(browser)
%{context: context, page: BrowserContext.new_page!(context)}
endand run it alone: mix test test/my_app/sign_up_test.exs, or mix test --only debug. The
call's launch_options merge over the module's (its own or its project's, else the config's)
one key at a time, so the other launch options stay. headless and channel, set at any level,
win over the launch options', as Playwright Test's: pass headless: false itself, not in
launch_options.
To step through a test with Page.pause/1, tag it @tag timeout: :infinity (or run mix test --trace): otherwise ExUnit's timeout ends the test mid-pause, closing its contexts. The same goes
for the Inspector: PWDEBUG=1 mix test --trace test/my_app/sign_up_test.exs:12 (see the
debugging guide).
Remote browsers
To run the browsers elsewhere (Playwright's Docker image, a grid), pass connect_options,
Playwright Test's connectOptions: the pool then connects to a Playwright server instead of
launching. It takes ws_endpoint and the options of Playwright.BrowserType.connect/3:
# config/test.exs
config :playwright, :test,
connect_options: [ws_endpoint: "ws://localhost:3000/", expose_network: "<loopback>"]Start the server as in Remote browsers. The local driver
still runs on_start, and contexts on the connected browser get Playwright Test's defaults and
are recorded, as on a launched one. The launch options, with headless and channel over them,
go to the server in the x-playwright-launch-options header, as Playwright Test sends them; the server applies those
it allows (headless, channel, slow_mo, ...). A connected browser and a launched one are
different browsers, also of one browser_name, and a lost connection is made again by the next
call. Each driver connects once, and the server launches a browser per connection, so each
worker has a browser of its own, as each Playwright Test worker does.
A call's connect_options win over the config's, and connect_options: nil launches. To switch
a suite from the shell, read an env var in the config:
# config/test.exs
config :playwright, :test,
connect_options: if(url = System.get_env("PW_TEST_CONNECT_WS_ENDPOINT"), do: [ws_endpoint: url])PW_TEST_CONNECT_WS_ENDPOINT=ws://localhost:3000/ mix test then runs on remote browsers.
The remote browser must reach your app: set base_url to a host the container resolves
(http://host.docker.internal:4000 on Docker Desktop), or pass expose_network: "<loopback>"
so the server tunnels localhost to your machine.