I currently have a scenairo whereby we run tests in CI mode and they fail intermittently.
I have tried to workout why but have run out of ideas for a solution
Key scenairo observations:
cy.wait()
we still have an issueCode below: (I have renamed endpoints)(
it("should display error if user not allowed", () => {
cy.server();
cy.route(
"POST",
"https://**/allowUser",
"fixture:allowUser.json"
);
cy.visit(url);
cy.get("#field1").type("suspended1");
cy.get("#submit-button").click();
cy.server({ status: 401 });
cy.route("POST", "https://**/allowUser", "fixture:notAllowed.json").as(
"notAllowed"
);
cy.get(".notAllowed").contains(
"Not Allowed."
);
});
I expects tests to run and pass
Cypress: 3.3.2
Headless using chrome
If clicking the submit button is what is causing the POST XHR, then you want to define the route BEFORE you click the button like below.
cy.server({ status: 401 });
cy.route("POST", "https://**/allowUser", "fixture:notAllowed.json").as(
"notAllowed"
);
cy.get("#submit-button").click();
Calling cy.route()
is essentially setting up a listener for XHR's - so that when it finds a matching route, it will have access to it. See this comment