首页 > 解决方案 > 量角器 - 如何验证元素不可见

问题描述

这个问题与这个问题中给出的解决方案密切相关

在我的测试脚本中,我需要转到登录脚本并需要注销,以防浏览器在应用程序中自动登录。因此,按照问题How to create a condition in protractor for when an element exists or not中提供的解决方案,我创建了这个脚本:

 beforeEach(function () {
    browser.driver.manage().window().maximize();
    browser.get(globalVariables.loginMain);
    globalVariables.User_Menu_Dropdown.isDisplayed().then(function(Login_Menu) {

        if (Login_Menu) {

            globalVariables.User_Menu_Dropdown.click();
            browser.wait(globalVariables.until.presenceOf(globalVariables.logOut_Button), 3000, 'The Logout menu too long to appear in the DOM');
            globalVariables.logOut_Button.click();
            browser.wait(globalVariables.until.presenceOf(globalVariables.Email_Input_box), 3000, 'The User Input box too long to appear in the DOM');
        } else {

            console.log("the app is on the login page")//do nothing

        }

    });

但是当我运行脚本时,我仍然收到以下错误 "Failed: No element found using locator: By(css selector, img[class="img-thumb-xs mr-1 align-middle"])".我在这里做错了什么?实现它的最佳方法是什么?

标签: javascriptjasmineprotractor

解决方案


您可以在您的情况下使用ExpectedConditions

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be no longer visible on the dom.
browser.wait(EC.invisibilityOf($('#abc')), 5000);

或者您可以使用not条件,这将导致相同的结果

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be no longer visible on the dom.
browser.wait(EC.not(EC.visibilityOf($('#abc'))), 5000);

推荐阅读