Protractor Check if Element Does Not Exist

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:23:38

Another option that worked a bit better for me, and uses protractor 'way' of doing things http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.all

element.all(by.css('.k-loading-image')).then(function(items) {
    expect(items.length).toBe(0);
});

(I wanted to check that a loading indicator had disappeared)

Got the thing working by using something I found in the docs:

expect(element(by.css('.switch')).isPresent()).to.become(false).and.notify(next);

Also uses assertions, so it doesn't break cucumberjs.

I got it working by doing this:

expect(element(by.css('css')).isPresent()).toBeFalsy();

none of these answers which include count() worked for me;

the typeof $$('.selector').count() is 'object'

you have to use the promise to pull the count value out like this.

const nameSelector = '[data-automation="name-input"]';
const nameInputIsDisplayed = () => {
    return $$(nameSelector).count()
        .then(count => count !== 0)
}
it('should not be displayed', () => {
    nameInputIsDisplayed().then(isDisplayed => {
        expect(isDisplayed).toBeFalsy()
    })
})

These answers do not wait for the element to disappear. In order to wait for it to disappear you need to use ExpectedConditions like below. InvisibilityOf detects whether an element has left the DOM. See it in the docs here: https://www.protractortest.org/#/api?view=ProtractorExpectedConditions.

  export const invisibilityOf = (el) =>
     browser.wait(ExpectedConditions.invisibilityOf(el) as any, 12000, 
    'Element taking too long to disappear in the DOM')
  const el = element(by.css(arg1))
  return invisibilityOf(el)

stalenessOf may be a good way to go: Protractor - ExpectedConditions.stalenessOf

For example you have a modal that is currently open. You close it and expect it to not be present:

element(by.css('.modal-dialog .cancel-button')).click();
browser.wait(EC.stalenessOf(element(by.css('.modal-dialog')), 60000);
expect(element(by.css('.modal-dialog')).isPresent()).toBeFalsy();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!