Cypress 我应该使用wait()等待对话框关闭吗?有更好的办法吗?

Cypress 我应该使用wait()等待对话框关闭吗?有更好的办法吗?,cypress,Cypress,我想测试一下,当我点击一个关闭模态对话框(ng材质)的按钮时,模态不存在 此测试实际上关闭了模态,但它通过了 it('should close the modal when the close button is clicked', () => { cy.get('#close-new-category').click(); cy.get('#new-category').should('exist'); }); 此测试也有效 it('sh

我想测试一下,当我点击一个关闭模态对话框(ng材质)的按钮时,模态不存在

此测试实际上关闭了模态,但它通过了

it('should close the modal when the close button is clicked', () => {
            cy.get('#close-new-category').click();
            cy.get('#new-category').should('exist');
});
此测试也有效

it('should close the modal when the close button is clicked', () => {
            cy.get('#close-new-category').click();
            cy.get('#new-category').should('not.exist');
});
如果我添加等待,此测试将失败

it('should close the modal when the close button is clicked', () => {
                cy.get('#close-new-category').click();
                cy.wait(500);
                cy.get('#new-category').should('exist');
});
这个测试如我们所料通过了,但是使用wait()是最好的方法吗?

it('should close the modal when the close button is clicked', () => {
            cy.get('#close-new-category').click();
            cy.wait(500);
            cy.get('#new-category').should('not.exist');
});

我之所以这么问,是因为文档中说这是一种反模式,应该有更好的方法。

在这些测试中不需要使用wait()。我认为这些示例测试太像单元测试,而不像端到端测试

案例#1通过的原因是,单击“关闭”后,模式仍然存在一段时间。在这种情况下,should()会立即进行检查,但仍然可以看到您的模态,因此它会传递并继续

案例#2也通过了,因为should()内置了重试逻辑,这是cypress的关键特性之一。单击close之后,should()立即检查并发现该模式确实存在。由于找到模式,它将等待并重试。最终,模态确实消失了,这应该()过去了


出于测试场景的目的,您只需要关心案例2,因为它测试您的模式是否消失。在通过should()之后,您可以继续进行测试的下一步,知道模态已经消失。它最初仍然存在的事实与您接下来要完成的任务无关。

我相信在这种情况下,使用
cy.wait()
更明确地说明测试的计时意图是有效的。