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 16, 2018 in Selenium by eLiJha
• 720 points
505 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 16, 2018 by king_kenny
• 3,650 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,350 points
395 views
0 votes
1 answer

Need to know how phantomjs execution is stopped

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

answered May 23, 2018 in Selenium by sniffy_god
• 780 points
25 views
0 votes
1 answer

Need to perform parallel execution (multiple browser sessions) with Selenium & TestNG by using @BeforeSuite

@Beforesuite annotated method runs before the testNG.XML ...READ MORE

answered Mar 30, 2018 in Selenium by nsv999
• 5,110 points
1,663 views
0 votes
1 answer

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,110 points
1,142 views
0 votes
1 answer
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,150 points
1,242 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,150 points
993 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 20, 2018 in Selenium by king_kenny
• 3,650 points
1,518 views