Network Mocking & API Testing
Control the network layer: mock unstable third parties, stub edge cases, and even run pure-API tests using Playwright's request context.
Network Mocking & API Testing video demo
A practical screen-share style walkthrough for chapter 4, showing how the Playwright idea works in TypeScript and Java.
Briefing focus
Why network mocking matters
This is a structured lesson briefing. Real video/audio can be added later as a media source.
Estimated time
10 min
- 1Why network mocking matters
- 2Pure API tests with the request context
Transcript brief
Control the network layer: mock unstable third parties, stub edge cases, and even run pure-API tests using Playwright's request context. The video brief explains the mental model first, then demonstrates the workflow using the course code samples, and finishes with reliability checks you can apply in CI.
Key takeaways
- Translate the concept into a maintainable Playwright test.
- Understand the TypeScript and Java equivalents without changing the test intent.
- Avoid the common source of flaky or slow end-to-end tests for this topic.
Why network mocking matters
Modern apps depend on dozens of external APIs. One flaky third-party is enough to break your suite. Playwright's route() API lets you intercept, modify or stub network calls — perfect for edge cases that are hard to reproduce live (500s, slow responses, deprecated schemas).
test('checkout handles Stripe 500 gracefully', async ({ page }) => {
await page.route('**/v1/payment_intents', (route) =>
route.fulfill({
status: 500,
contentType: 'application/json',
body: JSON.stringify({ error: 'internal_error' }),
})
);
await page.goto('/checkout');
await page.getByRole('button', { name: 'Pay now' }).click();
await expect(page.getByRole('alert')).toContainText(/payment failed/i);
});Pure API tests with the request context
Playwright can drive APIs directly — no browser needed. Use it to seed data, sanity-check contracts, or run an API-only pyramid layer alongside UI tests.
import { test, expect } from '@playwright/test';
test('GET /api/courses returns ISTQB', async ({ request }) => {
const res = await request.get('/api/courses?slug=istqb-foundation-essentials');
expect(res.status()).toBe(200);
const json = await res.json();
expect(json.course.title).toContain('ISTQB');
});Real-life scenario · E-commerce
Testing a Stripe outage without taking production down
Situation. A retailer's checkout went down for 9 minutes during Black Friday because their app crashed when Stripe returned 503. Their new Playwright test stubs a 503 from `**/v1/payment_intents` and asserts a graceful fallback message is shown. The test now runs on every PR.
Lesson. Mock your dependencies at the network boundary. You gain test speed, determinism and the ability to verify edge-cases you can't provoke safely in production.
Use HAR files for complex mocks
page.routeFromHAR('fixture.har') replays a previously-recorded network session. Ideal when you need to mock 40 endpoints with realistic payloads.