I try to create an E2E Cypress test in my react project.
I need to wait until the POST request return 200.
I don’t need to do the call, because the button that i click() do it.
i have this in the console:
that’s fine, but arrive late, and before this the test continue running and show me the error:
I have this test in this moment:
describe("E2E CognitiveTool", function () {
it("should crear respuesta simple sin problemas", function () {
cy.visit("http://localhost:3000");
cy.contains('[data-cy="topicName"]', 'CA2');
cy.get('[data-cy="accederResponsesCA2"]').click();
cy.get('[data-cy="crearResponse"]').should('exist')
.click();
cy.get('input[name="description"]')
.type('Simple Cypres Response');
cy.get('[name="typeResponse"]').first().check({ force: true });
cy.get('[name="lateral_W"]').first().check({ force: true });
cy.get('[name="rolViews"]').first().check({ force: true });
cy.get('[name="workLoadLevel"]').first().check({ force: true });
cy.get('[data-cy="continuar"]').click();
cy.get('input[id=0]')
.type('hola');
cy.get('input[id=1]')
.type('adios');
cy.get('input[id=2]')
.type('cypress');
cy.get('input[id=3]')
.type('jest');
cy.get('input[id=4]')
.type('testing');
cy.get('input[id=5]')
.type('automatico');
cy.get('[data-cy="testing"]').should('exist')
.click({ force: true }); /* here start the POST request and wait the 200 response, i would like to stop here until recieve the POST 200 */
cy.window()
.its('open')
.should('exist')
.get('[data-cy="continuar"]').click();
cy.get('input[name="title"')
.type('Titulo de respuesta simple');
cy.get('input[name="text"')
.type('TEXTO DE CYPRESS de respuesta simple');
cy.get('[data-cy="saveandedit"]').should('exist')
.click();
});
});
How can i do this?
Some idea? thanks for your time.
2
Answers
You can use
cy.wait()
to wait for intercepted calls to complete before continuing. In this case, we’d want to set up the intercept before the call is triggered:Then, after triggering the call, we can use
cy.wait()
.If the request takes a significant amount of time, you can increase the amount of time that
cy.wait()
waits for the request.I wrote this answer assuming that you did not need the 200 response, but just to wait for the response from the API, as your question read as if your test continued before the response was received.
You don’t have to explicitly wait for the XHR, just increase the timeout of the failing command.
Don’t chain
.get()
off.should()
, start a new chain. Include.should('be.visible')
before clicking.