Check if an item exists in Protractor

I am trying to use the code snippet below to check if the item that I am looking for exists, but all I get is "Error: no items were found using the locator: By (css selector, .icon-cancel)" , I want the program to execute the b () function

element(by.css('.icon-cancel')).isDisplayed().then(function(result) { if ( result ) { a(); } else { b(); } }); 
+5
source share
2 answers

isDisplayed() will fail if the item does not actually exist in the DOM tree. You need isPresent() :

 $('.icon-cancel').isPresent().then(function(result) { if ( result ) { a(); } else { b(); } }); 
+11
source

One possibility is that if an item is loaded dynamically, that item may not be loaded at the time your test runs. This way you can wait a few seconds for the item to be available.

 var EC = protractor.ExpectedConditions; var yourElement = element(by.css('.icon-cancel')); browser.wait(EC.presenceOf(yourElement), 5000); 
0
source

All Articles