3

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?

Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
user3188198
  • 111
  • 1
  • 2
  • 7

8 Answers8

6

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
4

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.

Brine
  • 3,733
  • 1
  • 21
  • 38
  • Cool, thanks. And for e.g if i check that after "x" minutes the element is not displayed anymore what do i need to use? – user3188198 Feb 08 '16 at 16:40
1

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");
 }
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
user3188198
  • 111
  • 1
  • 2
  • 7
1

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);
Joe
  • 678
  • 9
  • 24
0

I managed to find out a solution, using the protractor library.

var EC = protractor.ExpectedConditions;     browser.wait(EC.invisibilityOf(element(by.xpath(CastModule.PersonXpath))), 5000).then(function() {
            if (EC.invisibilityOf(element(by.xpath(x.LanguagesXpath)))) {
                console.log("Persons module is not present - as expected for this scenario");
            } else {
                throw new Error("Element STILL present");
            }
        });
    });
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
user3188198
  • 111
  • 1
  • 2
  • 7
0

You can also try below code to handle element displayed or not. Below code returns true or false according to the visibility of the element.

browser.wait(() => {
 return element(by.className("claasName")).isDisplayed()
      .then(
           (hasDisplayed) => {
              console.log("Has displayed: "+ hasDisplayed);
              return hasDisplayed === false;
            }
        );
   }, 5000)
        .then(
            () => {
                return true;
            },
            () => {
                return false;
            }
        )
Naren Chejara
  • 1,124
  • 10
  • 7
-1

To use presence of element use:

var elm =  element(CastModule.PersonXpath);
                elm.isPresent().then(function (present) {
                    if (present) {
                         element(CastModule.PersonXpath).then(function (labels) {

                        });

                    } else {
                        console.log('Element did not found');

                    }`enter code here`
-1
expect(elem.isNull===undefined).to.be.equal(true);
scopchanov
  • 7,966
  • 10
  • 40
  • 68