30. Simulating Backends with Network Interception

Mocking with page.route()

test("simulate 500 server error toast", async ({ page }) => {
  await page.route("**/api/checkout", (route) => {
    route.fulfill({
      status: 500,
      contentType: "application/json",
      body: JSON.stringify({ error: "Payment server timeout" })
    });
  });

await page.goto("/checkout");
await page.getByRole("button", { name: "Pay Now" }).click();
await expect(page.getByRole("alert")).toHaveText("Payment server timeout");
});

Task

  • Implement buildMockPayload:
  • 1. Accept data (object) and optional status (number, default 200).
  • 2. Return { status, contentType: "application/json", body: JSON.stringify(data) }.
πŸ’‘Common doubts
Did you like the problem?
Lesson 30 of 35
JavaScript
Loading code editor…
Next