-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How to send a trigger inside .then() to exit an outer loop prematurely? #2710
Comments
A quick and dirty fix is to write your code using async/await, then paste it into https://babeljs.io/en/repl to convert it (maybe keep your original code as a comment in the file). |
@ldiary I'm not totally sure what you want to do exactly when the 2 images is not found and I'd like to see the code above in the context of an Here is an example to making the test fail when the conditions of the request are not met in a loop of 49 pages. I like to reproduce the examples locally, so I've used a public API endpoint instead of your exact example. Is this helpful? cy.wrap([...Array(49).keys()]).each((page) => {
cy.request({
url: `https://jsonplaceholder.typicode.com/comments/${page + 1}`,
method: 'GET',
}).then((response) => {
if (response.body.id === 10) {
cy.log(10)
throw new Error('not enough images here')
}
})
})
}) |
@Herteby I was thinking if only async/await can be used with @jennifer-shehane thanks for response. After reading your example, I found out that it("Should provide products with multiple images", () => {
cy.wrap([...Array(49).keys()]).each((page) => {
return cy.request({
url: `https://myapi.com/1/products?page=${page + 1}`,
method: 'GET',
timeout: 70000,
failOnStatusCode: false
}).then((response) => {
let hasMulitpleImages = false
response.body.response.forEach(product => {
if (product.images.length > 2) {
// We found a product we can use for testing
// Use this product info to query another API endpoint (which is actually GraphQL)
// Assert that the second API endpoint returned correct values
hasMulitpleImages = true
}
})
if (hasMulitpleImages) return false // test completed successfully, let's exit the loop
if (page === 48 && hasMulitpleImages === false) {
// We never found a usable product, fail the test here
}
})
})
}) Cypress is giving me this error:
|
I finally found a workaround by re-structuring the test and using describe("Issue 2710: Workaround on exiting outer loop prematurely", () => {
before("Preparation", () => {
const findItemWithMultipleImages = (page) => {
cy.request({
url: `https://myapi.com/1/products?page=${page}`,
method: "GET",
timeout: 70000,
failOnStatusCode: false
}).then(response => {
const found = response.body.response.find(p => {
if (p.images.length > 2){
cy.wrap(p).as('foundItem')
return true
}
return false
})
if (!found && page < 49) {
findItemWithMultipleImages(page + 1)
}
})
}
cy.wrap(findItemWithMultipleImages(1)).as("Find an item with multiple images.")
})
it.only("Test for item with multiple images.", () => {
cy.get('@foundItem').then(item => {
// Start testing here
cy.log(item)
})
})
}) |
AFAIU, @ldiary found a workaround that works for his case by moving some of code into the |
THANK U SOOOO MUCH for this, I was struggling for days with the same issue till I found this reference! |
I tried to ask in gitter first but didn't get any suggestions. I realized I may not get any advise in gitter though, since this is somehow related to #1417 . I could have solved this problem in minutes if only Cypress have async/await, however as I am banging my head now for days, I wonder if someone could suggest me a workaround.
Here's what I want to achieve - scan an API endpoint using pagination and stop (or exit the loop) once I get a product with more than 2 images.
I want to exit the loop when
hasMultipleImages
is set to true. However, although .then() can access thehasMultipleImages
, whatever value is set by .then() gets ignored and theif(hasMultipleImages) break
never gets executed because inside theif
statementhasMultipleImages
always evaluate to the initial valuefalse
.I tried a lot of backflipping codes including this one, but nothing really worked.
Anyone has any suggestion for a workaround, please?
The text was updated successfully, but these errors were encountered: