gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:05:47 28 4
gpt4 key购买 nike

我想测试当我单击关闭模态对话框(ng Material )的按钮时该模态不存在。

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

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的关键特性之一。单击关闭后,should() 会立即检查并发现该模式确实存在。因为找到了模态框,所以它会等待并重试。最终模式确实消失了,并且 should() 通过了。

出于测试场景的目的,您只需要关心案例#2,因为它测试您的模态是否消失。在 should() 通过之后,您就可以继续测试的下一步,因为您知道模态已经消失。事实上,它最初仍然存在,对于您下一步想要完成什么并不重要。

关于cypress - 我应该使用 wait() 等待对话框关闭吗?有没有更好的办法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com