Cypress: Problem with multiple requests and route / wait

Created on 3 Feb 2019  Â·  17Comments  Â·  Source: cypress-io/cypress

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

needs investigating

All 17 comments

+1

I'm having the same issues when using GraphQL as it only has one endpoint, if you've figured out a solution please let me know :)

Any update on this?

I am having the same trouble on 3.3.1. Any update on this?

yep same here with v3.3.1. Noticed it with the same structure described by the original poster of this issue and also with something like:

cy.wait(@someApiCall).then(() => {
     // do some stuff

     cy.wait(@someApiCall.then(....

});

really strange side note: If I have Chrome Dev Tools open on the Chromium instance launched by Cypress it will behave as expected. The two calls act independently. If dev tools is closed, I 100% consistently get the behavior described in this issue

Same issue, with graphql, in my case some queries are running successfully (response 200 on network tab) before wait is even created. The network tab shows 10 successfully request, but wait is hanging on for the 4th one as if the request's never been invoked when it's already done behind.

@rhday74 Regarding your comment:

really strange side note: If I have Chrome Dev Tools open on the Chromium instance launched by Cypress it will behave as expected. The two calls act independently. If dev tools is closed, I 100% consistently get the behavior described in this issue

Is it possible you have "disable cache" checked in the network tab? This might be the difference.

I just encountered the same issue.

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
.

The above solution does not work for me. Any chance of getting this fixed? Or maybe there is another workaround?

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.

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)

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);

I think you can now use cy.wait('@postProperties.2').then((xhr) => { ... }) to wait and access data of the second call to that alias. It's an undocumented feature.

Also, cf. opened ticket on this topic

Hope this helps!!!

This works for me

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

@chdeliens What you are referencing doesn't seem to work as expected. From what I've tried, I could use

cy.wait('@postProperties.50').then(() => { ... })

I would land on then(...) directly. No matter if there's really 50 calls or if that number of responses is reached for given alias.

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
Was this page helpful?
0 / 5 - 0 ratings