API mocking questions come up in almost every SDET interview. They want to know if you understand when to mock, how to mock, and whether you have used it in real projects. Here are the most common questions with strong answers.
Q: What is API mocking in Playwright and why is it important?
A: API mocking means intercepting HTTP requests made by the browser and returning fake responses instead of letting them reach the real server. In Playwright, we use page.route() to intercept and route.fulfill() to return custom responses. It is important because it makes tests faster (no network latency), more reliable (no backend dependency), and enables testing edge cases like error states, empty data, and slow responses that are hard to reproduce with a real API.
Q: What is the difference between page.route() and page.on("request")?
A: page.route() is an interceptor -- it catches requests and lets you modify, fulfill, or abort them. The request does not proceed until your handler decides what to do. page.on("request") is a listener -- it observes requests passively without modifying them. The request proceeds normally. Use route() when you want to mock or block. Use on("request") when you want to monitor or log.
Q: How would you test an application that shows different UI for different user roles using API mocking?
A: I would mock the user profile API to return different roles. For example, mock /api/user to return { role: "admin" } in one test and { role: "viewer" } in another. Then verify that admin sees the admin panel, edit buttons, and delete buttons, while viewer only sees read-only content. This way I test all role-based UI without needing multiple real user accounts.
Q: What is a HAR file and how is it used in Playwright?
A: HAR stands for HTTP Archive. It is a JSON file that records all HTTP requests and responses during a browser session. In Playwright, you record a HAR with page.routeFromHAR("file.har", { update: true }), then replay it with page.routeFromHAR("file.har"). It is useful when you have complex flows with many API calls and you want realistic mock data without writing it by hand.
Q: How do you handle authentication in Playwright tests so that every test does not need to log in?
A: I use Playwright storageState feature. I create a setup project that logs in once and saves the auth state (cookies, localStorage) to a JSON file using page.context().storageState({ path: "auth.json" }). Then in playwright.config.ts, I configure all test projects to use this storageState file. Every test starts already authenticated. This saves 5-10 seconds per test.
Q: You have a test that sometimes passes and sometimes fails because the API is slow. How would you fix it?
A: Two options. First, I would use page.waitForResponse() to wait for the specific API call to finish before asserting on the UI. This replaces any hardcoded waits. Second, if the flakiness is caused by the real API being unreliable, I would mock it with page.route() and route.fulfill() so the test does not depend on the real API at all. The choice depends on whether this is a unit-level UI test (mock it) or an integration test (wait for it properly).
Q: Can you explain the difference between route.abort(), route.fulfill(), and route.continue()?
A: route.abort() kills the request -- the browser gets a network error, as if the server is unreachable. Use it to test offline scenarios or block unwanted resources. route.fulfill() returns a custom response you define -- status, headers, body. The real server is never contacted. Use it for mocking. route.continue() lets the request go to the real server, but you can modify headers, URL, or method on the way out. Use it to add auth headers or redirect requests.
Q: In your previous project, how did you decide which tests to mock and which to run against the real API?
A: I follow a testing pyramid approach. Most tests (80%) are mocked -- they test UI logic, error handling, edge cases, and rendering with controlled data. These run fast and are reliable. A smaller set (15%) are integration tests that hit the real staging API to verify the actual data flow works end-to-end. And a few (5%) are smoke tests in production-like environments. The key principle: mock for speed and coverage, use real APIs for confidence.
Key Point: In interviews, always connect mocking to real benefits: faster tests, better reliability, edge case coverage. Give specific examples from your projects.
Key Point: Know when to mock vs when to use real APIs. Explain the difference between abort, fulfill, and continue. Mention storageState for auth.
Answer all 5 questions, then submit to see your score.
1. What does route.abort() do in Playwright?
2. When should you set up page.route() relative to the action that triggers the API call?
3. What is the purpose of route.fetch() in Playwright?
4. How does Playwright reuse authentication state across tests?
5. What does routeFromHAR() with { update: true } do?