🌐 Network & cy.intercept()

Network & cy.intercept(): cy.intercept() (formerly cy.route(), deprecated since v6) is Cypress's API for capturing the network layer.

cy.intercept() (formerly cy.route(), deprecated since v6) is Cypress's API for capturing the network layer. It can be used in three ways: "stub" the real request by replacing it with fake data, "spy" on the request without modifying it, or simulate delay/errors. This is something you could never do in the Selenium world without standing up a separate proxy server (like BrowserMob Proxy).

Micro Lab: Cypress — API Intercept

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: Cypress — API Intercept

Determine which endpoint to capture: URL pattern or method + pattern

Set up: cy.intercept("GET", "**/api/users", { fixture: "users.json" }).as("getUsers")

Trigger cy.visit() or action, wait for the intercept to fire

Wait for the request with cy.wait("@getUsers")

Verify: cy.get("@getUsers").its("response.statusCode").should("eq", 200)

What is the Cypress API intercept setup order?

cy.intercept() Request Lifecycle

In Java, stubbing the network requires spinning up a separate HTTP server like WireMock/MockServer, managing port conflicts, and starting/stopping it around your tests. In Cypress, cy.intercept() captures the browser's network layer directly — there is no extra server.

This difference is a direct consequence of Cypress's "same-process" architecture — since the network layer is already inside the same browser, intercepting it becomes trivial.

🎬 Before the Request Even Leaves: How cy.intercept() Steps In