In protractor, browser.isElementPresent vs element.isPresent vs element.isElementPresent
Solution 1
All function in a similar way with subtle differences. Here are few differences that i found -
- Is an extension of
ElementFinder
and so waits for Angular to settle on page before executing any action. - It works when
elm
is anelement(locator)
orElementFinder
and notElementArrayFinder
. If multiple elements are returned using thelocator
specified then first element is checked if itisEnabled()
in the DOM. Doesn't take any parameter as input. - Works best with Angular pages and Angular elements.
- First preference to use whenever there is a need to find if an element is present.
elm.isElementPresent(subLoc)
- (When there is a sub locator to elm
)
- Is an extension of
ElementFinder
and so 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 parentelm
as a parameter. (only difference between this and theelm.isPresent()
) - Works best with Angular pages and Angular elements.
- First preference to use whenever 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 so doesn't wait for angular to settle. - Takes a
locator
or anelement
as a parameter and uses the first result if multiple elements are located using the same locator. - Best used with Non-Angular pages.
- First preference to use when testing on non-angular pages.
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...
Solution 2
I can't speak to which one is preferred but I was able to find the source code and examine it.
According to the docs, elm.isPresent()
and elm.isElementPresent()
are equivalent. Hope that helps.
There is a link to View code
just to the right of the title.
browser.isElementPresent(elm);
https://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.isElementPresent
/**
* Schedules a command to test if there is at least one descendant of this
* element that matches the given search criteria.
*
* @param {!(webdriver.Locator|webdriver.By.Hash|Function)} locator The
* locator strategy to use when searching for the element.
* @return {!webdriver.promise.Promise.<boolean>} A promise that will be
* resolved with whether an element could be located on the page.
*/
webdriver.WebElement.prototype.isElementPresent = function(locator) {
return this.findElements(locator).then(function(result) {
return !!result.length;
});
};
elm.isPresent();
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isPresent
/**
* Determine whether the element is present on the page.
*
* @view
* <span>{{person.name}}</span>
*
* @example
* // Element exists.
* expect(element(by.binding('person.name')).isPresent()).toBe(true);
*
* // Element not present.
* expect(element(by.binding('notPresent')).isPresent()).toBe(false);
*
* @return {ElementFinder} which resolves to whether
* the element is present on the page.
*/
ElementFinder.prototype.isPresent = function() {
return this.parentElementArrayFinder.getWebElements().then(function(arr) {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(function() {
return true; // is present, whether it is enabled or not
}, function(err) {
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
return false;
} else {
throw err;
}
});
}, function(err) {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
return false;
} else {
throw err;
}
});
};
elm.isElementPresent();
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isElementPresent
/**
* Same as ElementFinder.isPresent(), except this checks whether the element
* identified by the subLocator is present, rather than the current element
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()` is
* identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
*
* @see ElementFinder.isPresent
*
* @param {webdriver.Locator} subLocator Locator for element to look for.
* @return {ElementFinder} which resolves to whether
* the subelement is present on the page.
*/
ElementFinder.prototype.isElementPresent = function(subLocator) {
if (!subLocator) {
throw new Error('SubLocator is not supplied as a parameter to ' +
'`isElementPresent(subLocator)`. You are probably looking for the ' +
'function `isPresent()`.');
}
return this.element(subLocator).isPresent();
};
Related videos on Youtube
alecxe
"I am a soldier, at war with entropy itself" I am a Software Developer and generalist who is in love with the Python language and community. I greatly value clean and maintainable code, great software, but I know when I need to be a perfectionist and when it stands in a way of product delivery. I like to break things, to find new ways to break things, to solve hard problems, to put things under test and stress, and to have my mind blown by an interesting question. Some of my interests: Learning, Productivity, AI, Space Exploration, Internet of Things. "If you change the way you look at things, the things you look at change." - Wayne Dyer If you are looking for a different way to say "Thank you": Amazon wish list Pragmatic wish list Today I left my phone at home And went down to the sea. The sand was soft, the ocean glass, But I was still just me. Then pelicans in threes and fours, Glided by like dinosaurs, An otter basked upon its back, And dived to find another snack. The sun corpuscular and bright, Cast down a piercing shaft, And conjured an inspiring sight On glinting, bobbing craft. Two mermaids rose up from the reef, Out of the breaking waves. Their siren song was opium grief, Their faces from the grave. The mermaids asked a princely kiss To free them from their spell. I said to try a poet’s bliss. They shrugged and bid farewell. The sun grew dark and sinister, In unscheduled eclipse. As two eight-headed aliens Descended in their ships. They said the World was nice enough But didn’t like our star. And asked the way to Betelgeuse, If it wouldn’t be too far. Two whales breached far out to sea, And flew up to the sky, The crowd was busy frolicking, And didn’t ask me why. Today I left my phone at home, On the worst day, you’ll agree. If only I had pictures, If only you could see. Not everything was really there, I’m happy to confess, But I still have the memories, Worth more than tweets and stress. Today I left my phone at home, I had no shakes or sorrow. If that is what my mind can do, It stays at home tomorrow. Gavin Miller
Updated on June 12, 2022Comments
-
alecxe almost 2 years
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?
-
Ardesco over 8 yearsWhoever downvoted this tell us why, the answer looks good to me.
-
Michael Radionov over 8 yearsI think downvotes are for just copy-pasting source code without explaining anything.
-
alecxe over 8 yearsThat's the kind of answer I was hoping to get. Thank you very much! I find these 3 ways of presence check quite confusing since there are actually differences between the methods and it's kind of unclear what the differences are by just looking to how the methods are named. Plus, the differences are not properly documented. Hope this answer just filled the gap and would help others in the future.
-
alecxe over 8 yearsHaven't downvoted but haven't upvoted either. I think Michael is certainly right about the downvote reasons. Thank you for participating and looking into this anyway!
-
alecxe over 8 yearsThanks! That's the problem - there are actually 3 ways to do that in protractor and this is what the question is about.
-
JeffC over 8 yearsI did explain that two were equivalents... the rest I figured was pretty self-explanatory from the source. These are like 2- to 3-liner functions, excluding the error code in
elm.isPresent()
. shrugs