量角器,使用isDisplayed()我得到NoSuchElementError:找不到使用定位器的元素

Mik*_*kel 23 javascript jasmine angularjs angularjs-e2e protractor

在量角器2.0中,我正在检查expect()是否显示了一个元素.我希望是假的,但奇怪的是我得到以下错误:

NoSuchElementError:找不到使用locator找到的元素:By.id("userForm")

我的代码是:

describe('closeModal', function() {
    it('should close the alert that appears after registration.', function(){
        element(by.id('closeAlertModalButton')).click();
        expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
    });
});
Run Code Online (Sandbox Code Playgroud)

我明白我得到了那个错误,因为元素不再在页面上(我想要确认),但是我不应该得到错误而不是错误?

ale*_*cxe 32

isDisplayed()会检查一个元素是否可见,但你需要检查一个元素是否存在于DOM中,使用isElementPresent()isPresent():

expect(browser.isElementPresent(element(by.id('userForm')))).toBe(false);
expect(element(by.id('userForm')).isPresent()).toBe(false);
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • 你是"量角器天使!" 我从你的答案中学到了很多东西!谢谢!@alecxe (3认同)