32. Network Interception, Mock Responses & Fault Injection

Mocking Backend APIs with page.route

// Mock 500 internal server error on checkout:
page.route("/api/checkout", route -> {
    route.fulfill(new Route.FulfillOptions()
        .setStatus(500)
        .setContentType("application/json")
        .setBody("{"error": "Payment gateway unavailable"}"));
});

// Abort third-party tracking scripts to speed up tests:
page.route("
/*analytics.js", Route::abort);

// Modify response headers:
page.route("**/api/user", route -> {
route.resume(); // Pass through normally
});

Task

  • Implement mockEmptySearchResults:
  • 1. Accept Page page and String endpointPattern.
  • 2. Intercept via page.route and fulfill with status 200, contentType "application/json", and body "[]".
πŸ’‘Common doubts
Did you like the problem?
Lesson 32 of 38
Java
Loading code editor…
Next