Need to know which is better among browser isElementPresent vs element isPresent vs element isElementPresent

0 votes

I know that there are 3 diff ways to check if an element is present while testing with Protractor. My question is, are all these the same and can they be used interchangeably? And which one is the best usage?

var elm = element(by.id("myid"));

browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
May 17, 2018 in Selenium by eLiJha
• 770 points
1,923 views

1 answer to this question.

0 votes

All of these work very similar but with subtle differences. Those differences are:- 

elm.isPresent() -

This is an addition to ElementFinder and this waits basically for Angular to settle on page before executing that action.
It works when elm is an element(locator) or ElementFinder and not ElementArrayFinder. 
Works best with Angular pages and Angular elements. 
1st preference when there is a need to find if an element is present.

elm.isElementPresent(subLoc) - (When there is a sub locator to elm)

This is basically an extension to ElementFinder, and this waits for Angular to settle on page before executing any action.
Used to check the presence of elements that are sub elements of a parent. It takes a sub locator to the parent elm as a parameter. (only difference between this and the elm.isPresent())
Works best with Angular pages and Angular elements.
1st preference when there is a need to check if a sub element of a parent is present.

browser.isElementPresent(element || Locator) -

Is an implementation of webdriver and obviously doesn't automatically wait for angular to settle down.
Takes a locator or an element as a parameter and uses the first result if multiple elements are located using the same locator.
Best used with Non-Angular pages.
1st preference to use for testing non-angular or descent level dynamic pages.
All of the above checks for the presence of an element in DOM and returns a boolean result. Even though angular and non-angular features don't affect the usage of these methods, but its just more developer friendly. 
There's an added advantage when the method waits for angular to settle by default and helps avoid errors in case of angular like elementnotfound or stateelementreference exceptions, among others.

answered May 17, 2018 by king_kenny
• 3,710 points

Related Questions In Selenium

0 votes
1 answer

Which is better CSS or Xpath? Considering performance and cross browser testing

According to css has better performance compared ...READ MORE

answered Apr 17, 2018 in Selenium by Shubham
• 13,490 points
2,310 views
0 votes
1 answer

Need to know how phantomjs execution is stopped

Actually, the .close() method is not the ...READ MORE

answered May 24, 2018 in Selenium by sniffy_god
• 780 points
643 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,649 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,131 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,209 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,865 views
0 votes
1 answer
0 votes
1 answer

Need to scroll down for locating an Element with Selenium

There are a couple of options for ...READ MORE

answered Apr 21, 2018 in Selenium by king_kenny
• 3,710 points
12,945 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP