I just started using Protractor to write tests. I am wondering what the difference is between the isPresent()
and isDisplayed()
methods.
The API definitions
isPresent
isDisplayed
So... in what cases are they different?
Short answer is that isVisible is method of old Selenium RC and isDisplayed is method of Selenium 2. If you are talking about WebDrivers WebElement , it contains only isDisplayed() method, which by the doc: Is this element displayed or not?
isDisplayed() is capable to check for the presence of all kinds of web elements available. isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. isEnabled() is primarily used with buttons.
isSelected() This method is often used on radio buttons, checkboxes or options in a menu. It is used to determine is an element is selected. If the specified element is selected, the value returned is true. If not, the value returned is false.
How does Selenium WebDriver's isDisplayed() method work? We can work with isDisplayed() method in Selenium webdriver. This method checks if a webelement is visible on the page. If it is visible, then the method returns a true value, else it returns false.
isPresent is true if element exists in a page (in DOM), but can be hidden (display: none in css) isDisplayed is true only if isPresent is true and element is visible
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With