[英]Return a value from an async block in Cypress?
I am trying to return a value from a function where the return value is inside the then() block.我正在尝试从 function 返回一个值,其中返回值位于 then() 块内。 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.
我尝试返回 Promise 并解决它,但这也引发了错误。
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
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.