{超时:10000}的替代方法

Alternative to { timeout: 10000 }

当我点击按钮 "Add to cart" 时,值“2”没有保存,我的购物车中只有一件商品。问题是,赛普拉斯跑得很快。因为当我在这两个步骤之间等待一秒钟时,值 2 将被保存。

it('add two items to cart', function() {

    cy.get('.cx-counter-value', { timeout: 5000 })
      .clear()
      .type(2)
      .should('have.value', '2')

   // .wait(1000)

    cy.contains('Add to cart').click()
})

是否有“{timeout: 5000}”的替代方法来等待预期条件?

Cypress 没有像预期条件那样的 Selenium;相反,它具有更高级的概念,可以利用事件/ XHR 等,

例如,在您的情况下,我假设应用程序应该触发 XHR 调用以将 2 件商品添加到购物车。您可以等待 XHR 请求成功并验证您的购物车中是否存在 2 件商品,

// Wait for the route aliased as 'addToCart' to respond
// without changing or stubbing its response
cy.server()
cy.route('/cart/*').as('addToCart')
cy.visit('/cart/item/2')
cy.wait('@addToCart').then((xhr) => {
    cy.get('.cx-counter-value')
      .should('have.value', '2')
})

即使不是 XHR,也只需查看是否触发了您可以依赖的任何事件。

参考: https://docs.cypress.io/api/commands/wait.html#Alias