Testing Forms and Complex Interactions
What you'll learn: How to test multi-step forms, validation flows, and sequential user actions using Playwright's powerful interaction and assertion capabilities.
Beyond Simple Clicks
Real applications rarely involve single interactions. Users fill out registration forms, correct validation errors, navigate through wizards, and submit data—all in sequence. Testing these flows requires orchestrating multiple actions and verifying state changes at each step.
Multi-Step Form Testing
Think of testing complex forms like following a recipe: each step depends on the previous one completing successfully, and you need to verify the result at key checkpoints.
test('completes checkout flow', async ({ page }) => {
await page.goto('/checkout');
// Step 1: Shipping information
await page.getByLabel('Full Name').fill('Jane Doe');
await page.getByLabel('Email').fill('jane@example.com');
await page.getByLabel('Address').fill('123 Main St');
await page.getByRole('button', { name: 'Continue to Payment' }).click();
// Verify navigation occurred
await expect(page.getByRole('heading', { name: 'Payment' })).toBeVisible();
// Step 2: Payment details
await page.getByLabel('Card Number').fill('4242424242424242');
await page.getByLabel('Expiry').fill('12/25');
await page.getByLabel('CVV').fill('123');
await page.getByRole('button', { name: 'Place Order' }).click();
// Verify success
await expect(page.getByText('Order confirmed')).toBeVisible();
});
Testing Validation Errors
Test unhappy paths by triggering validation errors and verifying users can recover:
test('handles validation errors', async ({ page }) => {
await page.goto('/signup');
await page.getByLabel('Email').fill('invalid-email');
await page.getByRole('button', { name: 'Sign Up' }).click();
// Assert error appears
await expect(page.getByText('Please enter a valid email')).toBeVisible();
// Correct the error
await page.getByLabel('Email').fill('valid@example.com');
await page.getByRole('button', { name: 'Sign Up' }).click();
// Verify success
await expect(page).toHaveURL('/welcome');
});
Key Takeaway: Test forms as complete user journeys—fill fields sequentially, verify validation feedback appears and clears appropriately, and assert that successful submission produces the expected outcome.