Playwright Page Fixture Not Working Fix
In this tutorial, you'll learn about Playwright Page Fixture Not Working Fix. We cover key concepts, practical examples, and best practices.
Your Playwright test throws an error when trying to use page — the fixture is undefined or behaves unexpectedly across tests.
The Problem
// WRONG — using page in describe block instead of test
test.describe('user profile', () => {
page.goto('/profile'); // Error — page is not available here
// Fixtures are only available inside test blocks
});
TypeError: Cannot read properties of undefined (reading 'goto')
Fixtures like page are only available inside test() callbacks, not in describe() blocks.
Step-by-Step Fix
1. Use page fixture correctly
// RIGHT — page is available in test callback
import { test, expect } from '@playwright/test';
test('user profile heading', async ({ page }) => {
await page.goto('https://example.com/profile');
await expect(page.locator('h1')).toHaveText('User Profile');
});
2. Understand fixture lifecycle
// RIGHT — each test gets its own page
test('first test', async ({ page }) => {
await page.goto('https://example.com/page1');
// page is a new browser page
});
test('second test', async ({ page }) => {
// This page is different from the first test
await page.goto('https://example.com/page2');
});
3. Use context to create pages
// RIGHT — create additional pages
test('interact with multiple pages', async ({ context }) => {
const page1 = await context.newPage();
const page2 = await context.newPage();
await page1.goto('https://example.com');
await page2.goto('https://example.org');
expect(page1 !== page2).toBe(true);
});
4. Share page state with fixtures
// RIGHT — custom fixtures for shared state
import { test as base } from '@playwright/test';
const test = base.extend({
authenticatedPage: async ({ page }, use) => {
await page.goto('https://example.com/login');
await page.fill('[name="email"]', 'user@example.com');
await page.fill('[name="password"]', 'password');
await page.click('button[type="submit"]');
await page.waitForURL('**/dashboard');
await use(page);
},
});
test('dashboard shows data', async ({ authenticatedPage }) => {
await expect(authenticatedPage.locator('h1')).toHaveText('Dashboard');
});
Expected output:
✓ user profile heading (1.0s)
✓ first test (0.8s)
✓ second test (0.7s)
Prevention Tips
- Use fixtures only inside
test()callbacks - Each test gets an isolated page fixture
- Use
context.newPage()for multiple pages - Use custom fixtures for reusable setup (login, etc.)
- Fixtures are cleaned up automatically between tests
Common Mistakes with fixture page
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro