How can I test that an element does not exist on the page with Protractor?

14,738

Solution 1

The error should not be related to the checking for the absence of an element. Try the following:

var elm = element(CastModule.PersonXpath);
expect(browser.isElementPresent(elm)).toBe(false);

See also:

Solution 2

Yeah, testing for NOT visible can be sucky. You should be able to use isPresent(), which means in the dom, where isDisplayed() means it's actually visible, which I'm thinking is your issue. Try...

expect(element(CastModule.PersonXpath).isPresent()).toEqual(false);

You may also want to break this out into a method and use an Expected Condition.

Solution 3

To check for visibility (in case isDisplayed or isPresent isn't working), just use:

 if (!EC.invisibilityOf(ug.personXpath)) {
   throw new Error("Partner logo is not displayed");
 }

Solution 4

The error doesn't look like it's to do with the element being displayed. It looks like it's to do with page synchronization. Try ignoring the sync, then waiting for angular above the expect with:

browser.ignoreSynchronization = true;
browser.waitForAngular();
expect(element(CastModule.PersonXpath).isDisplayed()).toEqual(false);
Share:
14,738

Related videos on Youtube

user3188198
Author by

user3188198

Updated on July 09, 2022

Comments

  • user3188198
    user3188198 almost 2 years

    I am trying to test if an element is not present on a page.

    I have tried using the following method, but I get an error each time:

    Method:

    expect(element(CastModule.PersonXpath).isDisplayed()).toEqual(false);
    

    Error: Failed: Timed out waiting for Protractor to synchronize with the page after seconds. Please see https://github.com/angular/protractor/blob/master/docs/f ...

    What method do you recommend?

  • Brine
    Brine over 8 years
    Lulz @alecxe... you submitted before me!
  • 151291
    151291 over 6 years
    getting error element is not a function in karma report