cypress: Problem with multiple requests and route / wait

Current behavior:

I am using the alias of cy.route() and on the page there are 2 requests for the same alias. For some reason when I use the following code, it uses the data from the first request, not the second.

Desired behavior:

I would like it to use the data of the last request and not the first one.

Steps to reproduce: (app code and test code)

it("testara a selecao de precos", () => {
  let priceValue: any = 1000000;
  let maxValue: any = 1500000;

  cy.route('POST','/v1/realestate/properties/search?token=altopedroso')
    .as('postProperties')

  cy.get(`input[ng-model="vm.filters['price_gte']"]`).type(priceValue)
  cy.get(`input[ng-model="vm.filters['price_lte']"]`).click();

  cy.wait("@postProperties")
    .then((xhr) => {
      let request: any = xhr.request.body;
      let response: any = xhr.response.body;

      response = response.body;

      testClass.propTests(response.results, request.filters, response.count);
    });

  cy.get(`input[ng-model="vm.filters['price_lte']"]`).type(maxValue);
  cy.get(`input[ng-model="vm.filters['price_gte']"]`).click();

  cy.wait("@postProperties")
    .then((xhr) => {
      let request: any = xhr.request.body;
      let response: any = xhr.response.body;

      response = response.body;
      
      console.log('response', response)
      console.log('request', request)
    });
});

Versions

Cypress: 3.1.4 Chrome: 71

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 19
  • Comments: 20 (2 by maintainers)

Most upvoted comments

I found a solution where in you create multiple aliases for the same route

eg - cy.route(‘GET’, ‘/request’).as(‘req1’) cy.wait(‘@req1) cy.route(‘GET’, ‘/request’).as(‘req2’) cy.wait(’@req1) cy.route(‘GET’, ‘/request’).as(‘req3’) cy.wait('@req1)

On Wed, Nov 13, 2019 at 7:12 PM Daniel notifications@github.com wrote:

I just encountered the same issue.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/cypress-io/cypress/issues/3308?email_source=notifications&email_token=ACPOCFND7KUEBUMZPFKZRQTQTP74VA5CNFSM4GUASTCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOED6FW2Q#issuecomment-553409386, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACPOCFJQQJ72S65ZCP2PXTLQTP74VANCNFSM4GUASTCA .

any chance on update here? I’m approaching the same issue in which it’s impossible to rename alias, all post are running at the same time so for *2 it fails.

    cy.wait('@generatedImages1').then(handler)
    cy.route('POST', '**/images/offer/generated').as('generatedImages2')
    cy.wait('@generatedImages2').then(handler)
    cy.route('POST', '**/images/offer/generated').as('generatedImages3')
    cy.wait('@generatedImages3').then(handler)
    cy.route('POST', '**/images/offer/generated').as('generatedImages4')
    cy.wait('@generatedImages4').then(handler)
    cy.route('POST', '**/images/offer/generated').as('generatedImages5')
    cy.wait('@generatedImages5').then(handler)
  • it just feels bad, looks like ugly workaround

This works for me

cy.wait("@Request1");
cy.wait("@Request2");
cy.wait(0);
cy.wait("@Request2");

I ran into a similar situation today, but all I did was…

cy.wait('@loadingRequest', { requestTimeout: 60000 }).its('status').should('eq', 200)
cy.wait('@loadingRequest', { requestTimeout: 60000 }).its('status').should('eq', 200)

This is the closest doc I could find about it https://docs.cypress.io/api/commands/wait.html#Nesting


I guess, in the original issue description, I would make this line return a promise.

testClass.propTests(response.results, request.filters, response.count);

Above doesn’t seem to work for glob routes:

cy.route('GET', '/route/**').as('r1')
cy.wait('@r1)
cy.route('GET', '/route/**').as('r2')
cy.wait('@r2)

This is a pretty nasty bug.

The workaround is to redefine the same route with a different alias and use that new alias for each new wait:

cy.route('GET', '/route').as('r1')
cy.wait('@r1)
cy.route('GET', '/route').as('r2')
cy.wait('@r2)

Note that this is different from @prashantabellad’s example above which uses the same alias for all the waits.