Create custom Playwright fixtures for test setup
✓Works with OpenClaudeYou are a test automation engineer specializing in Playwright. The user wants to create custom Playwright fixtures that encapsulate reusable test setup logic and provide those fixtures to multiple tests.
What to check first
- Verify Playwright is installed:
npm list @playwright/test - Check your
playwright.config.tsorplaywright.config.jsexists in the project root - Ensure test files use
import { test } from '@playwright/test'(not Jest'stest)
Steps
- Create a
fixtures.tsfile in your tests directory (or dedicated folder liketests/fixtures/) - Import the base
testobject andexpectfrom@playwright/test - Define custom fixture functions using
test.extend()with an object containing fixture definitions - Each fixture receives a callback function with a
useparameter that yields the fixture value - Add fixture dependencies by including them in the callback parameters (e.g.,
page,context,browser) - Export the extended
testobject so other test files import it instead of the base test - In your test files, import the custom
testfrom your fixtures file and use fixture names as parameters - Run tests with
npx playwright test— Playwright automatically resolves and injects fixtures
Code
// tests/fixtures.ts
import { test as base, expect, Page } from '@playwright/test';
type TestFixtures = {
authenticatedPage: Page;
apiHelper: {
createUser: (name: string) => Promise<{ id: number }>;
deleteUser: (id: number) => Promise<void>;
};
};
export const test = base.extend<TestFixtures>({
authenticatedPage: async ({ page }, use) => {
// Setup: navigate and log in before test
await page.goto('https://app.example.com/login');
await page.fill('input[name="email"]', 'test@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await page.waitForNavigation();
// Yield the authenticated page to the test
await use(page);
// Teardown: log out after test
await page.click('[data-testid="logout-button"]');
},
apiHelper: async ({ page }, use) => {
// Create helper object with API methods
const apiHelper = {
async createUser(name: string) {
const response = await page.request.post('/api/users', {
data: { name },
});
return response.json();
},
async deleteUser(id: number) {
await page.request.delete(`/api/users/${id}`);
},
};
await use(apiHelper);
},
});
export { expect };
// tests/example.spec.ts
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
Snapshot Test Creator
Create snapshot tests for UI components
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
Test Data Factory
Create test data factories and fixtures
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.