Testing / Playwright Interview questions
How do you mock API responses in Playwright?
page.route() intercepts network requests matching a URL pattern and lets you fulfill them with custom data instead of hitting the real backend.
await page.route('**/api/users', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([{ id: 1, name: 'Ada' }]), }); }); await page.goto('/users');
Routes can also inspect the real request before deciding what to do - forwarding it unchanged with route.continue(), modifying headers, or aborting it entirely with route.abort() to simulate a network failure. This makes it possible to test loading states, empty states, and error handling deterministically, without depending on a real backend being in a specific state.
More Related questions...