当 cy.wait() 超时时我该如何做?

问题描述 投票:0回答:1

此代码等待响应并在响应到达后执行操作。 等待超时时我该如何做事?

    cy.wait('@login').then((interception) => {
        const token = interception.response.body.token
        Cypress.env('ACCESS_CODE', token)
    }); 
javascript cypress
1个回答
0
投票

在 Cypress 中实现这一点是不可能的,如果不进行一些操作来绕过

cy.wait()
并不真正支持条件结果这一事实。

但是,我们可以使用 cy.get()

 的未记录功能来获取所有别名调用并检查生成的数组的长度以确定我们的操作:

cy.intercept('/foo').as('foo'); // Cypress code that triggers the `/foo` call cy.get('@foo.all').then((foo) => { if (foo.length > 0) { // code to do if the call is made } else { // code to do if the call is not made } });
但是,

cy.get('@alias.all')

没有任何等待或重试机制,因此如果调用被触发但在调用
cy.get()
之前尚未完成,则该调用可能不会出现在yield数组中。

如果是这样的话,

我们可以吞下赛普拉斯的错误

cy.on('fail', (err, runnable) => { if (err.message.includes('route: `foo`')) { // check the error message // code to execute if call is not made return false // returning false makes sure the test does not fail at this step } throw err // throw the error if we don't return false earlier, to ensure other test failures are still raised })
两件重要的事情:

    使用
  • cy.on()
     而不是 
    Cypress.on()
     会将事件的更改限制为 
    特定测试。如果您想在整个测试套件中实现这一点,您需要在支持文件中执行此操作并使用 Cypress.on()
  • return false
    /
    throw err
     -> 包含这些,以便您的测试不会因指定的失败而失败,但会因意外的失败而失败。
现在,鉴于上述所有情况,我强烈建议

不要执行其中任何一项。它们是可能的,但理想的解决方案是以决定性且可重现的方式编写测试或应用程序。 (在理想的测试中,)是否拨打电话不应该存在不确定性。如果这是联系第三方资源,我可能会建议存根或模拟整个流程以消除这种歧义,并对第三方和您的应用程序之间的集成进行其他集中测试。

© www.soinside.com 2019 - 2024. All rights reserved.