8. Playwright Advanced: Fixtures, Hooks, and Parallel Testing
Advanced Playwright features enable sophisticated testing patterns. Fixtures provide reusable test setup. Hooks control test lifecycle. Parallel testing speeds up test execution. Understanding these features is essential for building scalable test suites.
The key insight is that Playwright is designed for large test suites. It supports parallel execution, sharding, and retries. Fixtures and hooks keep tests DRY. Understanding these advanced features is essential for production-grade test automation.
This module covers fixtures, hooks, parallel testing, and reporting.
Fixtures and Hooks
Fixtures are reusable test setup that can be shared across tests. Playwright provides built-in fixtures like page, context, and browser. You can also create custom fixtures. Hooks (beforeEach, afterEach, beforeAll, afterAll) run code at specific points in the test lifecycle.
Fixtures and hooks keep tests clean and DRY. They reduce duplication and make tests more maintainable. Use fixtures for common setup like logging in or creating test data. Use hooks for cleanup and teardown.
Parallel Testing and Sharding
Playwright runs tests in parallel by default. You can configure the number of workers. Sharding splits tests across multiple machines, useful for CI/CD. Parallel testing and sharding dramatically reduce test execution time.
For large test suites, parallel testing is essential. It can reduce test execution time from hours to minutes. Sharding is useful for CI/CD where multiple machines are available. Understanding parallel testing is essential for efficient test suites.
// playwright.config.js import { defineConfig } from '@playwright/test'; export default defineConfig({ // Run tests in parallel fullyParallel: true, // Number of workers workers: process.env.CI ? 4 : 2, // Retry failed tests retries: process.env.CI ? 2 : 0, // Test directory testDir: './tests', // Reporter reporter: [ ['html'], ['list'], ['json', { outputFile: 'test-results.json' }] ], }); // Sharding in CI // Run with: npx playwright test --shard=1/3 // This runs the first third of tests on this machine
Try It Yourself: Advanced Playwright Tests
Follow these steps to practice advanced Playwright features.
Step 1: Create a custom fixture// fixtures.js<br>import { test as base } from <strong><code>@playwright/test;
// Define a custom fixture for authenticated user
export const test = base.extend({
authenticatedPage: async ({ browser }, use) => {
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(https://example.com/login);
await page.fill(#email, test@example.com);
await page.fill(#password, password);
await page.click(#submit);
await use(page);
},
});
Step 2: Use the fixture// tests/dashboard.spec.js<br>import { test, expect } from <strong><code>../fixtures;
test(dashboard shows user data, async ({ authenticatedPage }) => {
await authenticatedPage.goto(https://example.com/dashboard);
await expect(authenticatedPage.locator(.username)).toHaveText(Test User);
});
Step 3: Add test hookstest.describe(<strong><code>User Management, () => {
test.beforeAll(async () => {
console.log(Setting up test data...);
});
test.afterAll(async () => {
console.log(Cleaning up test data...);
});
test(creates a user, async ({ page }) => {
// Test code
});
});
Step 4: Configure parallel testing
Update playwright.config.js:export default defineConfig({<br> fullyParallel: true,<br> workers: 4,<br> retries: 2,<br>});
Try It Yourself: Create a fixture for a database connection. Use it in multiple tests. Configure parallel testing and observe the speed improvement.
Knowledge Check
Ready to test your understanding of 8. Playwright Advanced: Fixtures, Hooks, and Parallel Testing?