Protractor Check if Element Does Not Exist

All we need is an easy explanation of the problem, so here it is.

I have a setting in my angular based website that turns a dropdown on and off. If it is off, then it does not show on the main page.

With Protractor, I need to check to see if this element is not present when the switch is off. However, I should not be thrown into Element Not Found Error, as it is one test in a set of many. How should I do this?

I have tried to do:

expect($$('.switch').count()).to.equal(0).and.notify(next);

But I am getting an AssertionError with this…

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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)

Method 2

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.

Method 3

I got it working by doing this:

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

Method 4

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()
    })
})

Method 5

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)

Method 6

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();

Method 7

if your switch is defined in a config, you could do this

let dropdownSwitch = config.switch

expect(await $('dropdown css').isPresent()).toBe(dropdownSwitch);

this will cover it’s verification in both scenarios, when it’s on or off

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply