如何在没有出现柏树错误的情况下检查元素是否不存在?

时间:2019-10-23 14:12:28

标签: javascript cypress

我正在使用赛普拉斯(Cypress)测试Web应用程序,我想在检查元素是否不退出后执行一些赛普拉斯命令。

我已经尝试过了,但是赛普拉斯会显示一条错误消息,指出该元素不存在,它将停止测试。

    // The user is not logged, so the "logout" button is not displayed
    if (!cy.get("a").contains("Logout")) { // <--Cypress will stop here
        // If "logout" button does not exist, then log in
        cy.get("a").contains("Login").click();
    }
    // continue with the test
    // ...

我要检查的是元素是否在没有出现错误的情况下不退出,有没有一种方法可以在不使用循环或长时间等待的情况下进行处理?

2 个答案:

答案 0 :(得分:0)

这很简单。基本上,您需要使用赛普拉斯的断言方法,即可以并且可以使用它。以下是有效的解决方案,您可以使用它。

ports = $("#edit_display_control_ports > li > input").map(function(currentValue, index){ 
  return index.value;}
).toArray(); 

让我知道您是否遇到任何问题。

答案 1 :(得分:0)

This可能还会回答您的问题。