Fixing “context and page fixtures are not supported in beforeAll” in Playwright
In a Playwright E2E test, if you write page as an argument to test.beforeAll, the test fails with "context" and "page" fixtures are not supported in "beforeAll". page and context are created per test, and at the time beforeAll runs they do not exist yet. You are most likely to hit this when you want to gather shared setup such as logging in or opening the top page before the tests.
There are two fixes, depending on what you want to do. If you only want each test to open the same page, move the code to beforeEach. If you want one page shared across several tests, create the page yourself from browser in beforeAll. I reproduced the error in a test environment and confirmed that both fixes remove it.
The test environment was Ubuntu 24.04, Node.js 22.23.5, @playwright/test 1.63.0 (Chromium 158.0.8127.36, headless), and TypeScript 5.9.3. For the page, I used a small server built with Node.js's http module (an HTML page with only a heading, a count, and an "Add" button, plus a JSON response at /api/items), started through webServer. I did not test Firefox or WebKit, UI mode, or Playwright versions earlier than 1.63.0.
Reproducing the error
The config starts the server with webServer and sets baseURL to http://localhost:3000. In the test file tests/error/counter.spec.ts, I put the step that opens the top page in test.beforeAll, and wrote two tests that check the heading and the count.
import { test, expect } from '@playwright/test';
test.beforeAll(async ({ page }) => {
await page.goto('/');
});
test('見出しが出る', async ({ page }) => {
await expect(page.getByRole('heading', { level: 1 })).toHaveText('猫の手カウンター');
});
test('最初は0点', async ({ page }) => {
await expect(page.locator('#n')).toHaveText('0');
});
When I ran it, the first test failed and the second one did not run. The exit code was 1.
$ npx playwright test tests/error/
Running 2 tests using 1 worker
✘ 1 tests/error/counter.spec.ts:7:5 › 見出しが出る (0ms)
- 2 tests/error/counter.spec.ts:11:5 › 最初は0点
1) tests/error/counter.spec.ts:7:5 › 見出しが出る ──────────────────────────────────────────────────────
Error: "context" and "page" fixtures are not supported in "beforeAll" since they are created on a per-test basis.
If you would like to reuse a single page between tests, create context manually with browser.newContext(). See https://aka.ms/playwright/reuse-page for details.
If you would like to configure your page before each test, do that in beforeEach hook instead.
Error Context: test-results/error-counter-見出しが出る/error-context.md
1 failed
tests/error/counter.spec.ts:7:5 › 見出しが出る ───────────────────────────────────────────────────────
1 did not run
The failure is reported on the test that checks the heading, but the test body itself is not the problem. The duration is 0ms because it stopped in beforeAll, before the test body ran. The output has no file line number for the error, so I looked for the "beforeAll" in the message to find the cause.
The message has three lines. The first says that the context and page fixtures are created per test, so they cannot be used in beforeAll. The second says that to reuse one page across tests, you create the context yourself with browser.newContext(). The third says that to set up the page before each test, you do it in beforeEach. Those are the two fixes.
When I put context in the argument of test.afterAll instead, it failed with the same message, with "beforeAll" replaced by "afterAll". In that case, the test failed even though its body was empty and did nothing.
Why page cannot be used in beforeAll
The page and browser you receive as test arguments are provided by a mechanism called fixtures. In the official built-in fixtures table, page and context are described as isolated to a single test, and browser as shared between tests. In the Playwright 1.63.0 source, browser is also created once per worker (scope: "worker").

beforeAll runs once, before any test. At that point, no test's page has been created yet. In the Playwright 1.63.0 source, the code that creates a context checks whether beforeAll or afterAll is currently running, and throws this error if so.
The request fixture for API calls is also created per test, but using it as an argument to beforeAll did not cause an error. A test that called request.get('/api/items') in beforeAll and used the result passed. The error only occurs for context and page, as the message says.
If each test should start fresh, move the code to beforeEach
If you want every test to start from the top page, you only need to change beforeAll to beforeEach. The only difference from the file that caused the error is this one line.
$ diff tests/error/counter.spec.ts tests/fix-each/counter.spec.ts
3c3
< test.beforeAll(async ({ page }) => {
---
> test.beforeEach(async ({ page }) => {
When I ran it, the error was gone and both tests passed.
$ npx playwright test tests/fix-each
Running 2 tests using 1 worker
✓ 1 tests/fix-each/counter.spec.ts:7:5 › 見出しが出る (114ms)
✓ 2 tests/fix-each/counter.spec.ts:11:5 › 最初は0点 (75ms)
2 passed (785ms)
beforeEach runs for each test, so it can receive page. The example in the test.beforeEach reference also calls page.goto() inside beforeEach. As the built-in fixtures table says, page is isolated per test, so nothing from a previous test, such as a button press, carries over.
If you want to reuse one page, create it with browser.newPage()
Sometimes you want the state from one test to carry over to the next. In that case, beforeAll receives browser instead of page, and you create the page yourself with browser.newPage(). Store the page in a variable in the file and close it in afterAll. This follows the official example of reusing a single page between tests. I put this in tests/fix-new-page/counter.spec.ts.
import { test, expect, type Page } from '@playwright/test';
test.describe.configure({ mode: 'serial' });
let page: Page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/');
});
test.afterAll(async () => {
await page.close();
});
test('1回押すと1点', async () => {
await page.getByRole('button', { name: '追加' }).click();
await expect(page.locator('#n')).toHaveText('1');
});
test('もう1回押すと2点', async () => {
await page.getByRole('button', { name: '追加' }).click();
await expect(page.locator('#n')).toHaveText('2');
});
When I ran it, there was no error and both tests passed. The second test showed a count of 2, which means the result of the first test's button press was still on the same page.
$ npx playwright test tests/fix-new-page
Running 2 tests using 1 worker
✓ 1 tests/fix-new-page/counter.spec.ts:16:5 › 1回押すと1点 (81ms)
✓ 2 tests/fix-new-page/counter.spec.ts:21:5 › もう1回押すと2点 (31ms)
2 passed (820ms)
According to the browser.newPage() reference, browser.newPage() creates the page inside a new context, and closing that page also closes the context. As the error message suggests, you can also create a context with browser.newContext() and then a page with context.newPage(). The reference recommends the browser.newContext() approach for production code and test frameworks.
The line test.describe.configure({ mode: 'serial' }) at the top of the file makes the tests in this file run one at a time, in the order they are written. If tests run in parallel (for example with fullyParallel), beforeAll runs once per test, as explained in the official parallel execution docs, so the page cannot be shared. I checked the difference between serial mode and parallel mode in Running Playwright tests one at a time in order.
A test that reuses a page depends on the result of the previous test. The official docs also explain that writing tests so each one is independent lets them run more efficiently and be retried more easily. If you do not need to carry state over, choose the beforeEach approach.
Do not hide the shared page behind the page argument in tests
If you use the shared-page approach, keeping { page } in a test's arguments does not raise the error, but it causes a different failure. I changed only the second test in the file above to the following and ran it.
test('もう1回押すと2点', async ({ page }) => {
The first test passed. The second test waited for the "Add" button and failed with a 30-second timeout. (I replaced the path with /path/to/app and omitted part of the output.)
$ npx playwright test tests/shadow
Running 2 tests using 1 worker
✓ 1 tests/shadow/counter.spec.ts:16:5 › 1回押すと1点 (80ms)
✘ 2 tests/shadow/counter.spec.ts:21:5 › もう1回押すと2点 (30.0s)
1) tests/shadow/counter.spec.ts:21:5 › もう1回押すと2点 ─────────────────────────────────────────────────
Test timeout of 30000ms exceeded.
Error: locator.click: Test timeout of 30000ms exceeded.
Call log:
- waiting for getByRole('button', { name: '追加' })
…(中略)
at /path/to/app/tests/shadow/counter.spec.ts:22:50
…(後略)
The { page } argument is a new page that Playwright created for this test. It has the same name as the file-level page variable, so inside the test the argument is what gets used. That new page has not opened anything, so the button cannot be found. When you reuse a page, remove page from the test's arguments and write async () =>.





