日本語

How to Run Playwright Tests One by One in Order

​​Playwright​E2E Testing,TypeScript

If you write test.describe.configure({ mode: 'serial' }) at the top of a Playwright test file, the tests in that file run one at a time, in the order they are written. This keeps E2E tests from running in parallel and getting out of order when a later test depends on the result of an earlier one, such as registering a product, checking it, and then deleting it at the end.

In my test environment, three tests that started at the same time with no setting ran one after another, each taking about 1.4 seconds, once I specified mode: 'serial'. I also checked how mode: 'default' and --workers=1 differ from it, focusing on what happens when a test fails.

Note

The test environment was Ubuntu 24.04, 4 logical CPUs, Node.js 22.23.5, and @playwright/test 1.63.0. The tests were written in TypeScript and run only with Chromium (headless). The test page was built in place with page.setContent(), and I did not connect to a server. I did not run them on a CI service, and I did not try Firefox or WebKit.

Without any setting, the three tests start at the same time

To check this, I prepared a config file with fullyParallel: true and workers: 3. fullyParallel is the setting that runs tests in the same file in parallel as well.

ts
// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  workers: 3,
  reporter: 'list',
  projects: [{ name: 'chromium', use: { browserName: 'chromium' } }],
});

There are three tests. Each one presses a "Process" button and waits one second for "Done" to appear. Before and after each test, I wrote the time and worker number to a file called order.log (record and runStep are helper functions in a separate file). The second test is written to fail on purpose only when the environment variable FAIL_STEP2 is set.

ts
// tests/parallel/steps.spec.ts
import { test, expect } from '@playwright/test';
import { record, runStep } from '../helper';

test.beforeEach(async ({}, info) => record(info, 'start'));
test.afterEach(async ({}, info) => record(info, 'end'));

test('1. 商品を登録する', async ({ page }) => {
  await runStep(page);
});

test('2. 在庫を確認する', async ({ page }) => {
  await runStep(page);
  expect(process.env.FAIL_STEP2 ? 'エラー' : '完了').toBe('完了');
});

test('3. 商品を削除する', async ({ page }) => {
  await runStep(page);
});

When I ran it, the three tests started at the same time on three workers. In the order.log below, the times are converted to seconds from when the first test started.

text
$ npx playwright test tests/parallel
Running 3 tests using 3 workers
  ✓  2 [chromium] › tests/parallel/steps.spec.ts:11:1 › 2. 在庫を確認する (1.5s)
  ✓  3 [chromium] › tests/parallel/steps.spec.ts:16:1 › 3. 商品を削除する (1.5s)
  ✓  1 [chromium] › tests/parallel/steps.spec.ts:7:1 › 1. 商品を登録する (1.6s)
  3 passed (2.1s)

 0.00s start worker=0 parallel/steps.spec.ts 1. 商品を登録する
 0.01s start worker=1 parallel/steps.spec.ts 2. 在庫を確認する
 0.01s start worker=2 parallel/steps.spec.ts 3. 商品を削除する
 1.56s end   worker=2 parallel/steps.spec.ts 3. 商品を削除する
 1.57s end   worker=1 parallel/steps.spec.ts 2. 在庫を確認する
 1.59s end   worker=0 parallel/steps.spec.ts 1. 商品を登録する

In this run too, "Delete product" (1.56 seconds) finished before "Register product" (1.59 seconds).

Specifying mode: 'serial' at the top of the file

In the same test file, I added mode: 'serial' with test.describe.configure. This one line is the only change.

ts
// tests/serial/steps.spec.ts (otherwise the same as tests/parallel/steps.spec.ts)
test.describe.configure({ mode: 'serial' });

The config file still has fullyParallel: true and workers: 3, but the tests ran one at a time, in the written order, on a single worker. Each test started only after the previous one had finished.

text
$ npx playwright test tests/serial
Running 3 tests using 1 worker
  ✓  1 [chromium] › tests/serial/steps.spec.ts:8:1 › 1. 商品を登録する (1.4s)
  ✓  2 [chromium] › tests/serial/steps.spec.ts:12:1 › 2. 在庫を確認する (1.4s)
  ✓  3 [chromium] › tests/serial/steps.spec.ts:17:1 › 3. 商品を削除する (1.4s)
  3 passed (4.8s)

 0.00s start worker=0 serial/steps.spec.ts 1. 商品を登録する
 1.48s end   worker=0 serial/steps.spec.ts 1. 商品を登録する
 1.51s start worker=0 serial/steps.spec.ts 2. 在庫を確認する
 2.92s end   worker=0 serial/steps.spec.ts 2. 在庫を確認する
 2.94s start worker=0 serial/steps.spec.ts 3. 商品を削除する
 4.32s end   worker=0 serial/steps.spec.ts 3. 商品を削除する

The total time grew from 2.1 seconds in the parallel run to 4.8 seconds. If you write it inside test.describe(), only that group runs in order.

A chart that lays out the three runs as horizontal bars. With no mode setting, the three tests run at the same time from 0 seconds. With mode: 'serial', tests 1, 2, and 3 run one after another, 1.4 seconds each. When the second test fails, its bar is pink and the third test does not run.
Each test drawn from its start to its end along the timeline of the run

If a test fails midway, the remaining tests do not run

With mode: 'serial', if a test fails midway, the tests after it do not run. When I made the second test fail with FAIL_STEP2=1, the third test's line started with -, and the summary ended with 1 did not run. I have left out the detailed failure message.

text
$ env FAIL_STEP2=1 npx playwright test tests/serial
Running 3 tests using 1 worker
  ✓  1 [chromium] › tests/serial/steps.spec.ts:8:1 › 1. 商品を登録する (1.4s)
  ✘  2 [chromium] › tests/serial/steps.spec.ts:12:1 › 2. 在庫を確認する (1.4s)
  -  3 [chromium] › tests/serial/steps.spec.ts:17:1 › 3. 商品を削除する
  …(中略)
  1 failed
    [chromium] › tests/serial/steps.spec.ts:12:1 › 2. 在庫を確認する ─────
  1 did not run
  1 passed (3.3s)

The official serial mode documentation also says that if one test fails, all the tests after it are skipped. This fits cases where running the delete test after a failed stock check would not make sense.

mode: 'default' also runs in order, but moves on after a failure

To turn off parallel execution for a specific file while keeping fullyParallel: true in the config file, the official documentation on opting out of fully parallel mode lists mode: 'default'.

ts
// tests/default/steps.spec.ts (otherwise the same as tests/parallel/steps.spec.ts)
test.describe.configure({ mode: 'default' });

When every test passed, the tests ran one at a time on a single worker, as with mode: 'serial', and the whole run took 4.7 seconds. The difference appeared when I made the second test fail.

text
$ env FAIL_STEP2=1 npx playwright test tests/default
Running 3 tests using 1 worker
  ✓  1 [chromium] › tests/default/steps.spec.ts:8:1 › 1. 商品を登録する (1.4s)
  ✘  2 [chromium] › tests/default/steps.spec.ts:12:1 › 2. 在庫を確認する (1.4s)
  ✓  3 [chromium] › tests/default/steps.spec.ts:17:1 › 3. 商品を削除する (1.4s)
  …(中略)
  2 passed (5.2s)

 0.00s start worker=0 default/steps.spec.ts 1. 商品を登録する
 1.46s end   worker=0 default/steps.spec.ts 1. 商品を登録する
 1.48s start worker=0 default/steps.spec.ts 2. 在庫を確認する
 2.88s end   worker=0 default/steps.spec.ts 2. 在庫を確認する
 3.27s start worker=1 default/steps.spec.ts 3. 商品を削除する
 4.74s end   worker=1 default/steps.spec.ts 3. 商品を削除する

The third test also ran and passed. The worker number changed from 0 to 1. As the official documentation on retries explains, when a test fails, the worker process is discarded along with its browser, and the remaining tests continue on a new worker.

Setting Runs in order? When a test fails midway
mode: 'serial' Yes The remaining tests do not run
mode: 'default' Yes The remaining tests run on a new worker

A retry starts again from the beginning of the group

When I ran a file with mode: 'serial' using --retries=1, the first test, which had passed, was run again.

text
$ env FAIL_STEP2=1 npx playwright test tests/serial --retries=1
Running 3 tests using 1 worker
  ✓  1 [chromium] › tests/serial/steps.spec.ts:8:1 › 1. 商品を登録する (1.4s)
  ✘  2 [chromium] › tests/serial/steps.spec.ts:12:1 › 2. 在庫を確認する (1.4s)
  -  3 [chromium] › tests/serial/steps.spec.ts:17:1 › 3. 商品を削除する
  ✓  4 [chromium] › tests/serial/steps.spec.ts:8:1 › 1. 商品を登録する (retry #1) (1.4s)
  ✘  5 [chromium] › tests/serial/steps.spec.ts:12:1 › 2. 在庫を確認する (retry #1) (1.4s)
  -  6 [chromium] › tests/serial/steps.spec.ts:17:1 › 3. 商品を削除する (retry #1)
  …(中略)
  1 passed (6.7s)

The official documentation on the serial mode section of retries also says that the tests in a group are retried together. In this example, the run that took 3.3 seconds without retries took 6.7 seconds.

Reusing the page from an earlier test in the next test

If the reason you want tests to run in order is that you want to carry over the page state from an earlier test, you can open the page in beforeAll and reuse it across the tests in the group. The official serial mode example uses this pattern too. Here I used a page where the number of items in the cart goes up each time a button is pressed. (The page HTML is omitted.)

ts
// tests/shared/cart.spec.ts
test.describe.configure({ mode: 'serial' });

let page: Page;

test.beforeAll(async ({ browser }) => {
  page = await browser.newPage();
  await page.setContent(cartPage);
});

test.afterAll(async () => {
  await page.close();
});

test('1点目を入れる', async () => {
  await page.getByRole('button', { name: 'カートに入れる' }).click();
  await expect(page.locator('#count')).toHaveText('カート 1点');
});

test('2点目を入れる', async () => {
  await page.getByRole('button', { name: 'カートに入れる' }).click();
  await expect(page.locator('#count')).toHaveText('カート 2点');
});

When I ran this as is, both tests passed (2 passed). I placed a copy with mode: 'serial' changed to mode: 'parallel' in tests/shared-parallel/cart.spec.ts and ran it. The two tests then ran on different workers. In parallel mode, beforeAll runs once for each test (official documentation on parallelizing tests in a single file). As a result, the page for the second test did not have the first item in it, and the test failed.

text
$ npx playwright test tests/shared-parallel
Running 2 tests using 2 workers
  ✓  1 [chromium] › tests/shared-parallel/cart.spec.ts:28:1 › 1点目を入れる (77ms)
  ✘  2 [chromium] › tests/shared-parallel/cart.spec.ts:33:1 › 2点目を入れる (5.1s)
  …(中略)
    Expected: "カート 2点"
    Received: "カート 1点"
Point

The official documentation advises against using serial. Writing each test so it can run on its own is more efficient, and it lets each test be retried separately (official documentation on the serial mode section of retries).

Other files stay parallel even when one file is serial

mode: 'serial' only applies inside the file (or describe) where you write it. When I ran a serial file and a parallel file together with --workers=4, the serial file ran one test after another on worker 3 alone, while the three tests in the parallel file ran at the same time on workers 0 to 2.

text
$ npx playwright test tests/serial tests/parallel --workers=4
Running 6 tests using 4 workers
…(中略)
  6 passed (5.0s)

 0.00s start worker=0 parallel/steps.spec.ts 1. 商品を登録する
 0.01s start worker=3 serial/steps.spec.ts 1. 商品を登録する
 0.02s start worker=1 parallel/steps.spec.ts 2. 在庫を確認する
 0.03s start worker=2 parallel/steps.spec.ts 3. 商品を削除する
 1.58s end   worker=1 parallel/steps.spec.ts 2. 在庫を確認する
 1.59s end   worker=2 parallel/steps.spec.ts 3. 商品を削除する
 1.60s end   worker=0 parallel/steps.spec.ts 1. 商品を登録する
 1.61s end   worker=3 serial/steps.spec.ts 1. 商品を登録する
 1.68s start worker=3 serial/steps.spec.ts 2. 在庫を確認する
 3.07s end   worker=3 serial/steps.spec.ts 2. 在庫を確認する
 3.09s start worker=3 serial/steps.spec.ts 3. 商品を削除する
 4.47s end   worker=3 serial/steps.spec.ts 3. 商品を削除する

To run every test one by one, use –workers=1

If you want to run every test one at a time across all files, add --workers=1 (the same as workers: 1 in the config file). When I ran tests/parallel and tests/default together, the six tests ran in sequence on one worker and took 8.8 seconds.

text
$ npx playwright test tests/parallel tests/default --workers=1
Running 6 tests using 1 worker
  ✓  1 [chromium] › tests/default/steps.spec.ts:8:1 › 1. 商品を登録する (1.4s)
  ✓  2 [chromium] › tests/default/steps.spec.ts:12:1 › 2. 在庫を確認する (1.4s)
  ✓  3 [chromium] › tests/default/steps.spec.ts:17:1 › 3. 商品を削除する (1.4s)
  ✓  4 [chromium] › tests/parallel/steps.spec.ts:7:1 › 1. 商品を登録する (1.4s)
  ✓  5 [chromium] › tests/parallel/steps.spec.ts:11:1 › 2. 在庫を確認する (1.4s)
  ✓  6 [chromium] › tests/parallel/steps.spec.ts:16:1 › 3. 商品を削除する (1.4s)
  6 passed (8.8s)

I wrote tests/parallel first in the command, but tests/default ran first. According to the official documentation on test order, when parallel execution is turned off, test files run in alphabetical order. In this check too, default came before parallel.

--workers=1 makes every test run one at a time, so even tests that could run in parallel become slower. Each of these is six tests of about 1.5 seconds each (one file that runs in order and one file that runs in parallel). The earlier section that ran the serial and parallel files with --workers=4 took 5.0 seconds, while this section with --workers=1 took 8.8 seconds. If only some files need to run in order, adding mode: 'serial' to those files kept the total time shorter.

​Posted by 86