Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for waiting on the Nth request/response in cy.intercept() #7663

Open
flotwig opened this issue Jun 10, 2020 · 7 comments
Open

Add support for waiting on the Nth request/response in cy.intercept() #7663

flotwig opened this issue Jun 10, 2020 · 7 comments
Labels
pkg/driver This is due to an issue in the packages/driver directory topic: cy.intercept() type: enhancement Requested enhancement of existing feature

Comments

@flotwig
Copy link
Contributor

flotwig commented Jun 10, 2020

Current behavior:

cy.route has (undocumented) support for waiting on the Nth request/response:

cy.wait('@foo.1') // first response
cy.wait('@foo.request.1') // first request
cy.wait('@foo.response.5') // fifth response

cy.route2 does not have this.

Desired behavior:

Implement this in cy.route2, so that the following tests can pass:

context.skip('indexed aliases', function () {
it('can wait for things that do not make sense but are technically true', function () {
cy.route2('/foo')
.as('foo.bar')
.then(() => {
$.get('/foo')
})
.wait('@foo.bar.1')
.wait('@foo.bar.1') // still only asserting on the 1st response
.wait('@foo.bar.request') // now waiting for the next request
})
it('can wait on the 3rd request using "alias.3"', function () {
cy.route2('/foo')
.as('foo.bar')
.then(() => {
_.times(3, () => {
$.get('/foo')
})
})
.wait('@foo.bar.3')
})
it('can timeout waiting on the 3rd request using "alias.3"', function (done) {
cy.on('fail', (err) => {
expect(err.message).to.contain('No response ever occurred.')
done()
})
cy.route2('/foo')
.as('foo.bar')
.then(() => {
_.times(2, () => {
$.get('/foo')
})
})
.wait('@foo.bar.3', { timeout: 100 })
})
})

Depends on #687

@flotwig flotwig added pkg/driver This is due to an issue in the packages/driver directory topic: cy.intercept() labels Jun 10, 2020
@flotwig flotwig changed the title Add support for Add support for waiting on the Nth request/response in cy.route2 Jun 10, 2020
@flotwig flotwig added the type: enhancement Requested enhancement of existing feature label Jun 10, 2020
@flotwig
Copy link
Contributor Author

flotwig commented Jun 10, 2020

@quad5
Copy link

quad5 commented Nov 2, 2020

Hi, #687 is now closed. Any chance to get this enhancement ticket on to the implementation queue. This enhancement would help with the issue I reported (#9034). Thanks.

@jennifer-shehane jennifer-shehane changed the title Add support for waiting on the Nth request/response in cy.route2 Add support for waiting on the Nth request/response in cy.intercept() Jan 8, 2021
@vojta-k
Copy link

vojta-k commented Jan 27, 2021

Hi! This feature could potentially resolve our issue #14703 - !! if it would also include the possibility to select not just a specific n-th call from the start, but also the LAST one (e.g. by passing n=-1)

@vala84no
Copy link

vala84no commented Mar 9, 2021

The ability to get the last one seems likely to cause subtle bugs. How do you know there isn't another request about to come through and how do you know that one isn't the one the developer wanted? Unless you can guarantee that the request you want has already happened you can't feasible get the "last" request and know its what you're after. It seems extremely likely that people will use that incorrectly. At least Nth request is far more likely to be deterministic.

@mohamedatef2020
Copy link

@thor84no I believe waiting on the last request is the more realistic scenario.
If I'm on a page with an api that returns a list of element and I'm performing filtering/sorting action I will send multiple api calls to same endpoint with different parameters but I'll always need to wait on the last sent request to assert the displayed data after performing any of the actions.

It would be like:

cy.intercept("**list/).as("list")
getListRequest()
cy.wait(@list).then(assertDefaultData())
sortListAction()
cy.wait(@list).then(AssertSortedData())
filterListAction()
cy.wait(@list).then(assertFilteredData())

@vala84no
Copy link

vala84no commented Feb 24, 2022

@mohamedatef2020 but how could Cypress possibly know that it's actually the last request? It can't. You have to tell it how many requests there will be and it then has to wait for the nth request. At any given moment the most recent request is the last request with a possibility of a "last" request coming soon. That never changes.

If you look at what I responded to that post suggested using -1 as an indication that we should wait for whatever is the last request. That has nothing in common with your example code which is effectively wait for the 1st request, then the 2nd, etc.

I.e. your example is repeatedly wait for one more request, not wait for the last request.

@mohamedatef2020
Copy link

@thor84no I agree with you on that. I meant to mention latest request not last.
For now cy.wait() only returns the first request no matter how many requests where sent inside the test and how many times I wrote cy.wait() in my code. all of them will refer to the first matched request not the latest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/driver This is due to an issue in the packages/driver directory topic: cy.intercept() type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

No branches or pull requests

7 participants