Route Interception
Intercept and modify requests when you need control over backend behavior.
await page.route('**/api/**', route => route.continue());A focused guide to the advanced Playwright features that separate basic scripts from production-ready automation.
Intercept and modify requests when you need control over backend behavior.
await page.route('**/api/**', route => route.continue());Mock unstable or expensive dependencies to make tests deterministic.
await page.route('**/orders', async route => {
await route.fulfill({ status: 200, body: JSON.stringify({ ok: true }) });
});Playwright supports file upload and download flows cleanly.
await page.setInputFiles('input[type=file]', 'fixtures/sample.pdf');Capture alerts, confirms, and prompts with dialog handlers.
page.on('dialog', async dialog => {
await dialog.accept();
});Use frame locators when interaction moves into an iframe.
const frame = page.frameLocator('#payment-frame');
await frame.getByLabel('Card number').fill('4111111111111111');Handle popups and tabs through page events.
const [popup] = await Promise.all([
context.waitForEvent('page'),
page.getByText('Open').click(),
]);Persist state to avoid repeating UI login in every test.
await context.storageState({ path: 'auth.json' });Mock environment-dependent behavior directly from the browser context.
await context.setGeolocation({ latitude: 28.6139, longitude: 77.2090 });
await context.grantPermissions(['geolocation']);Device emulation and screenshots are key for responsive and visual workflows.
await page.screenshot({ path: 'home.png', fullPage: true });