back arrow

How to wait for many http request with Cypress

30 - 11 - 2022

I think half a year ago I have figured out how to deal with cypress wait. So for graphql I will interfere it add stick an alias on the request. The function go like this

Cypress.Commands.add("interceptQuery", (opName) => {
  cy.intercept("POST", "/graphql", (req) => {
    // eslint-disable-next-line no-prototype-builtins
    if (
      req.body.hasOwnProperty("query") &&
      req.body.query.includes(`query ${opName}`)
    ) {
      req.alias = opName;
    }
  });
});

And in the test cases, it can go like this

  it("Should be able to load more item by clicking Xem them btn", function () {
    ...
    cy.interceptQuery("GetCategory");
    cy.interceptQuery("GetCart");
    cy.visit("/instantdeal");
    ...

    cy.wait("@GetCategory");

    ...
    cy.wait("@GetCart");
    ...
  });

It gets better at least I don’t have to complain like here. But someone at my company suggest using invoke to handle many http requests