I just started using Protractor to write tests. I am wondering what the difference is between the isPresent()
and isDisplayed()
methods.
isDisplayed
resolves to whether the element is visible or not, but throws an exception if it is not in the DOM.
isPresent
resolves to whether it is there in the DOM or not, regardless of whether it is actually visible or not. It doesn't throw an exception.
The following code can be used to avoid the exception that isDisplayed throws if the element is not found in the DOM :
function isVisible(e) {
var deferred = protractor.promise.defer();
if (e) {
e.isDisplayed().then(
// isDisplayed Promise resolved
function(isDisplayed) {
deferred.fulfill(isDisplayed);
},
// Silencing the error thrown by isDisplayed.
function(error) {
deferred.fulfill(false);
}
);
}
else {
deferred.reject(new Error('No element passed'));
}
return deferred.promise;
}
Even an object with both the visibility and presence can be passed while resolving, for example :
deferred.fulfill({
visible: isDisplayed,
present: true
});
However, this won't work well with expect statements.