Ever had a UI test fail because the backend was slow, down or returning different data today? Mocking the API fixes that. You tell Playwright what the server should say, and your test checks how the page reacts.
Why mock an API in a UI test?
- Speed: no waiting on a real server.
- Stability: the same data every run.
- Hard cases: you can force errors, empty lists or slow responses that are difficult to trigger for real.
The basic pattern with page.route
test('shows products from a mocked API', async ({ page }) => {
await page.route('**/api/products', route =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify([{ id: 1, name: 'Laptop' }])
})
);
await page.goto('/shop');
await expect(page.getByText('Laptop')).toBeVisible();
});
Set the route before you navigate, otherwise the page may make the request before your mock is ready.
Simulating an error
await page.route('**/api/products', route =>
route.fulfill({ status: 500, body: 'Server error' })
);
await page.goto('/shop');
await expect(page.getByText('Something went wrong')).toBeVisible();
Modifying a real response
Sometimes you want the real call but with one field changed. Fetch it, edit it, then fulfil.
await page.route('**/api/user', async route => {
const response = await route.fetch();
const json = await response.json();
json.plan = 'premium';
await route.fulfill({ response, json });
});
A word of caution
Do not mock everything. If every call is faked, your test proves little about the real system. Mock the shaky or hard-to-reach parts, and keep a few true end-to-end tests. Related reading: network interception and API testing in Playwright.
How to apply this in practice
Reading about playwright mock api is a good start, but progress comes from applying it. Choose one small task this week and finish it end to end. Write down what you tried, what failed and what you changed. Repeat with a slightly harder task. A few weeks of this habit builds more confidence than reading many articles.
- Open a demo site built for practice and automate one complete user flow
- Write the test using locators based on roles and visible text
- Run it in headed and headless mode and read the trace if it fails
- Refactor repeated steps into a helper or page object
Common mistakes to avoid
Learners working on playwright mock api often slow themselves down in similar ways. Watch for these:
- Using brittle CSS or XPath selectors when role locators are available
- Adding fixed waits instead of relying on auto-waiting
- Sharing state between tests so they depend on order
- Ignoring reports and traces when a test fails
- Automating everything in the UI when an API check would be faster
Next steps
Keep momentum on playwright mock api by turning what you read into a habit.
- Pick one action from this guide and finish it within three days
- Write a short note about what you learned and what is still unclear
- Ask a mentor or peer to review your work and suggest one improvement
- Revisit this topic after two weeks and compare your progress
Checklist before you move on
Before treating playwright mock api as done, check yourself against this list.
- My tests pass reliably on repeated runs
- I use meaningful locators and assertions
- Reports or traces are available for failures
- My project is on GitHub with a clear README
Want to learn this hands-on?
Reading helps, but nothing beats building it with someone who answers your questions. Our Playwright training in Hyderabad is project-based, and you can sit in on a free demo class first. If you are aiming for AI-led engineering roles, take a look at our FDE training in Hyderabad too. You can also reach us on +91 75595 67655.

