Back to cheat sheet hub

Playwright Advanced Cheat Sheet

A focused guide to the advanced Playwright features that separate basic scripts from production-ready automation.

Playwright Module 4 Network + auth Mobile + visual

Route Interception

Intercept and modify requests when you need control over backend behavior.

await page.route('**/api/**', route => route.continue());

API Mocking

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 }) });
});

Files

Playwright supports file upload and download flows cleanly.

await page.setInputFiles('input[type=file]', 'fixtures/sample.pdf');

Dialogs

Capture alerts, confirms, and prompts with dialog handlers.

page.on('dialog', async dialog => {
  await dialog.accept();
});

Frames

Use frame locators when interaction moves into an iframe.

const frame = page.frameLocator('#payment-frame');
await frame.getByLabel('Card number').fill('4111111111111111');

Multiple Pages

Handle popups and tabs through page events.

const [popup] = await Promise.all([
  context.waitForEvent('page'),
  page.getByText('Open').click(),
]);

Authentication

Persist state to avoid repeating UI login in every test.

await context.storageState({ path: 'auth.json' });

Permissions and Geolocation

Mock environment-dependent behavior directly from the browser context.

await context.setGeolocation({ latitude: 28.6139, longitude: 77.2090 });
await context.grantPermissions(['geolocation']);

Mobile and Visual

Device emulation and screenshots are key for responsive and visual workflows.

await page.screenshot({ path: 'home.png', fullPage: true });