I am trying to return a value from a function where the return value is inside the then() block. Cypress throws a bunch of errors that I am mixing async and sync code. I tried returning a Promise and resolving it, but that threw up errors too.
cy.get('.btn.btn-primary')
.each(function ($el, index, $list) {
// Lot of code
if (price < minPrice) minPrice = price
})
.then(() => {
cy.log(minPrice); //This works fine
return minPrice; //This throws ERROR
})
You can use an alias to save the value and then later use it something like this.
cy.get('.btn.btn-primary')
.each(function ($el, index, $list) {
// Lot of code
if (price < minPrice) minPrice = price
})
.then(() => {
cy.log(minPrice) //This works fine
cy.wrap(minPrice).as('minPrice') //Saved as alias
})
//In your test you can use it as
cy.get('@minPrice').then((minPrice) => {
//Access minPrice here
cy.log(minPrice) //prints minPrice
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.