【问题标题】:Return a value from an async block in Cypress? [duplicate]从赛普拉斯的异步块返回一个值? [复制]
【发布时间】:2021-11-30 17:13:06
【问题描述】:

我正在尝试从返回值位于 then() 块内的函数返回一个值。 赛普拉斯抛出了一堆错误,我正在混合异步和同步代码。我尝试返回一个 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
  })

【问题讨论】:

标签: javascript cypress


【解决方案1】:

您可以使用别名来保存该值,然后再像这样使用它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多