我正在编写赛普拉斯测试以登录网站。有username
和password
字段和一个Submit
按钮。大多数登录都很简单,但有时会首先出现一个警告对话框,必须将其关闭。
我试过这个:
cy.get('#login-username').type('username');
cy.get('#login-password').type(`password{enter}`);
// Check for a possible warning dialog and dismiss it
if (cy.get('.warning')) {
cy.get('#warn-dialog-submit').click();
}
哪个工作正常,除了如果没有出现警告则测试失败:
CypressError: Timed out retrying: Expected to find element: '.warning', but never found it.
然后我尝试了这个,它失败了,因为警告出现的速度不够快,所以Cypress.$
没有找到任何东西:
cy.get('#login-username').type('username');
cy.get('#login-password').type(`password{enter}`);
// Check for a possible warning dialog and dismiss it
if (Cypress.$('.warning').length > 0) {
cy.get('#warn-dialog-submit').click();
}
检查元素是否存在的正确方法是什么?如果找不到元素,我需要类似的东西cy.get()
不会抱怨。