Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor, with isDisplayed() I get NoSuchElementError: No element found using locator

In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:

NoSuchElementError: No element found using locator: By.id("userForm")

My code is:

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

I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn't I get a false and not a error?

like image 600
Mikel Avatar asked May 07 '15 11:05

Mikel


3 Answers

isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():

expect(browser.isElementPresent(element(by.id('userForm')))).toBe(false);
expect(element(by.id('userForm')).isPresent()).toBe(false);

See also:

  • How do I test if an img tag exists?
  • Use element by css to check if element exists in Protractor
like image 199
alecxe Avatar answered Nov 10 '22 13:11

alecxe


This error is part of WebDriver behavior. For such cases you should better use isPresent or isElementPresent

like image 2
Vasiliy Vanchuk Avatar answered Nov 10 '22 15:11

Vasiliy Vanchuk


If element visible do A if not visible do B, disregard exception if element not found:

element.isDisplayed().then(function(visible){
    if (visible) {
        // do A when element visible
    }else{
        // do B when element not visible 
    }
}, function () {
    //suppress exception if element is not found on page
});
like image 2
Sergiy Podgorniy Avatar answered Nov 10 '22 14:11

Sergiy Podgorniy