In protractor, there are, basically, 3 ways to check if an element is present:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
Are these options equivalent and interchangeable, and which one should be generally preferred?
All function in a similar way with subtle differences. Here are few differences that i found -
ElementFinder
and so waits for Angular to settle on page before executing any action. elm
is an element(locator)
or ElementFinder
and not ElementArrayFinder
. If multiple elements are returned using the locator
specified then first element is checked if it isEnabled()
in the DOM. Doesn't take any parameter as input.elm.isElementPresent(subLoc)
- (When there is a sub locator to elm
)
ElementFinder
and so waits for Angular to settle on page before executing any action.sub locator
to the parent elm
as a parameter. (only difference between this and the elm.isPresent()
)browser.isElementPresent(element || Locator)
-
webdriver
and so doesn't wait for angular to settle.locator
or an element
as a parameter and uses the first result if multiple elements are located using the same locator.All of the above checks for the presence of an element in DOM and return a boolean
result. Though angular and non-angular features doesn't affect the usage of these methods, but there's an added advantage when the method waits for angular to settle by default and helps avoid errors in case of angular like element not found or state element reference exceptions, etc...