QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › API testing

What is cy.request and how does it differ from cy.intercept?

cy.request sends an HTTP request itself from the Cypress Node process, while cy.intercept only captures the requests the page makes. So cy.request is not subject to CORS or the same-origin policy, needs no open page, and cy.intercept cannot see it. Cookies from its response land in the browser, so a login through cy.request logs in the next cy.visit. It suits API tests and data setup.

Cypress · API testing

You need to create an order without the UI, but the test waits for a request nobody sends. Which command do you use?

Two colleagues write a test for placing an order. The first wants to prepare data quickly, so they set cy.intercept and wait for its alias, and the test times out: nothing was sent. The second wants to check that the “Place order” button sends the right body, and calls cy.request, which has nothing to do with the page. Both mixed up two commands that are similar only in that they touch HTTP.

How it works and why

cy.request does not run in the browser. The request goes from the Cypress Node process, so web-security rules do not apply: no CORS, no same-origin policy, and no page needs to be open. Cookies from the browser store for that URL are added to the request, and Set-Cookie from the response is saved in the browser. So a login through cy.request makes the next cy.visit logged in. The price of this independence: cy.intercept does not see such requests, and they are absent from the browser Network tab. They are visible only in the Command Log.

cy.intercept works the other way: it sends nothing itself, it only watches the requests the application makes and can modify them, replace the response or just count them. Where exactly the interception happens depends on the browser and the Cypress version, but for a test one thing matters: page traffic is intercepted, not cy.request calls.

Two HTTP commands, two different roles

cy.request

Sends a request itself

Runs from the Cypress Node process. No CORS, cookies are synced with the browser. For API tests, login and data setup.

cy.intercept

Sends nothing

Watches the requests the page makes and can replace the response. For mocks and checking what the front end sent.

Figure 1. These are not alternatives to each other but different tools: a request from the test versus watching the page’s requests.

Example: from confusion to the right choice

The wrong test waits for a request that does not exist:

// wrong: interception calls nothing, the alias never gets a hit
cy.intercept('POST', '/api/orders').as('create');
cy.wait('@create'); // waits for a request nobody makes

The right way is to split the roles: cy.request prepares data, and cy.intercept together with a UI action checks the front end:

// 1. cy.request SENDS the request itself and returns the response
cy.request('POST', '/api/orders', { items: [7] }).then((res) => {
  expect(res.status).to.eq(201);
});

// 2. cy.intercept WAITS for a request the page will make
cy.intercept('POST', '/api/orders').as('create');
cy.get('[data-cy=submit]').click();
cy.wait('@create').its('response.statusCode').should('eq', 201);

Now the first test checks the API itself (status, body, headers), and the second checks that the UI sent what it should and that the server answered as expected. They answer different questions, so neither command replaces the other.

Limits and alternatives

cy.request needs a URL: a relative path works with baseUrl set or after a cy.visit (Cypress then uses the visited page’s origin), otherwise write an absolute address. The response is returned whole, with no streaming. It cannot check what “the user sees” as a result of the request, the UI is for that. In Playwright the counterpart is the request fixture (APIRequestContext): no browser is started there either, so API tests are faster, and page.request shares cookies with the page.

Typical mistakes

  • Waiting for a cy.intercept alias for a request made by cy.request: nobody intercepts it.
  • Checking CORS through cy.request: it has none, so the check proves nothing.
  • Creating data through UI clicks when cy.request is enough: the test is longer and more fragile.
  • Testing only through cy.request and assuming the UI works too: the front end may send a different request.

Check yourself

A test makes a cy.request to /api/orders and then waits for cy.wait('@create') from an intercept on that URL. Why does this not work?

Answer

cy.request goes from the Node process, not from the page, so cy.intercept does not see it and the alias gets no request. The alias waits for a request the application will make.

Why does a login through cy.request make the next cy.visit logged in?

Answer

Cypress stores Set-Cookie from the response in the browser and the next page sends those cookies. That is how a cookie session works. A token in the response body does not behave like that, you pass it yourself.

In an interview. Junior: cy.request sends the request itself from the Node process, so CORS does not get in its way. I use it for API tests, login and data setup. cy.intercept sends nothing, it intercepts the page’s requests. Middle/Senior: I split the roles: cy.request for arranging and direct API checks, cy.intercept together with a UI action to check what the front end sent. I remember that intercept does not see cy.request, and that response cookies go straight into the browser.

More articles in this chapter