Network Mocking

Replace real backend calls with fast fake ones during a test.

Playwright Automation Module 4 Lesson 2
10 min read

What you'll learn

  • Block or change network calls.
  • Make tests faster and less flaky.
  • Test empty, error, and happy paths.

Network Mocking

Replace real backend calls with fast fake ones during a test.

Big Picture

One small picture can make this idea easier to hold.

Real network

  • Can be slow.
  • Can fail for reasons outside the test.
  • Uses live data.
  • Good for true end-to-end checks.

Mocked network

  • Very fast.
  • Test controls the response.
  • Uses fake data.
  • Good for stable feature tests.

VerdictMocking is best when you want speed and control.

How It Moves

Short steps make the flow easier to see.

1

Watch the request

Find the API call the page makes.

Choose a reply

Pick the data or error you want to send back.

3

Intercept it

Swap the real call for your fake response.

Run the test

Check the page with the controlled data.

Step By Step

This is the same idea, stretched across time.

  1. 1

    Observe traffic

    See which request the page sends.

  2. 2

    Replace the response

    Send fake data back from the test.

  3. 3

    Assert the UI

    Check what the user sees.

  4. 4

    Test more states

    Try error and empty cases too.

One Small Model

Think of this like a tiny card you can keep in your pocket.

MockRoute

5 fields
url:"/api/todos"
method:"GET"
status:"200"
body:"[{ id: 1, title: 'Buy milk' }]"
scenario:"happy / empty / error"
The test chooses the URL, the status, and the data.

Quick Check

Question 1 of 10 correct

Why mock network calls in tests?

Map It

One more picture helps you see where this lesson matters most.

Mocking choices

Realism
Fake backend

Live and loose

Slow and flaky test

Best balance

Mock only one call

Heavy realism

Almost full app flow

Full lab mode

All data comes from the test

Live backend
Little controlFull control
Response control
Use more mocking when speed and reliability matter most.

Final Quiz

Question 1 of 30 correct

What does network mocking do?