Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the isPresent and isDisplayed methods

I just started using Protractor to write tests. I am wondering what the difference is between the isPresent() and isDisplayed() methods.

The API definitions

  1. isPresent

  2. isDisplayed

So... in what cases are they different?

like image 408
翁鹏飞 Avatar asked Jan 23 '15 21:01

翁鹏飞


People also ask

What is the difference between isVisible and isDisplayed?

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?

What is the difference between isDisplayed and isEnabled in Webdriver?

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.

How do you use isSelected method in Selenium?

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.

Is displayed method in Selenium Webdriver?

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.


2 Answers

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

like image 190
sap1ens Avatar answered Oct 08 '22 00:10

sap1ens


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.

like image 20
abhishek89m Avatar answered Oct 07 '22 22:10

abhishek89m