Difference between Selenium visibility methods
Selenium provides several methods you can use during a test execution to confirm that a specific object exists. Familiarize yourself with these methods and the differences between them.
Click a method to view more information.
This method determines if an element is selected or not. It returns true if the element is selected and false if it is not. It is widely used on check boxes, radio buttons, and options in a select.
The above statement, when used with a radio button, will return true if the radio button is selected. Otherwise, it will return false. The above statement, when used with a check box option, will return true if the check box option is selected. Otherwise, it will return false. Use the assertTrue( ) predefined method of JUnit to take the above result and:
- Pass the test when the result is true
- Fail the test when the result is false
The statement to verify whether the radio button is selected will look like the following:
assertTrue("Radio button is not selected", _driver.findElement(By.xpath("XPathForLocatingRadioButton")).isSelected( );
The statement to verify whether the check box is selected will look like the following:
assertTrue("Check Box is not selected", _driver.findElement(By.xpath("XPathForLocatingCheckBox")).isSelected( );
This method determines if an element is enabled or not. It returns true if element is enabled (all elements apart from disabled input elements) and false if otherwise.
isEnabled() checks for the disabled attribute on the button element. If the attribute disabled is not present, it returns true, meaning that if you never add this attribute to disabled buttons and instead add the value disabled to the button's class, isEnabled() will always return true.
If you need to determine whether the button is enabled or disabled based on a class, you will need to instead check for the existence of a button with the disabled class (find it by class name, xpath, or CSS selector) to decide what state the button is in.
isEnabled() syntax
boolean file_name = driver.findElement(By.xpath("//input[@name='the_name']")).isEnabled();
System.out.print(file_name);
We can use the isEnabled() method with an if condition to take action based on the element's enabled status on the page or software web application.
This method basically tests if the element we are looking for is present somewhere on the page. Observe that isElementPresent() won't mind even if our element is not visible.
Code samples
#1
isElementPresent() // positive test case
if(isElementPresent(By.cssSelector(your_Value)))
System.out.println("The element present");
else
System.out.println("this element is missing");
#2 Checks the availability using size()
if(driver.findElements(By.id("Value)).size()>0)
{
System.out.println("The element present");
}else
{
System.out.println("this element is missing");
}
#3
!isElementPresent() // negative test caseif(!isElementPresent(By.xpath("Value")))
{
System.out.println("The element present");
} else
{
System.out.println("this element is missing");
}
This method determines if an element is displayed on the screen or not , that is whether its width and height are greater than zero, it isn't hidden by CSS, and so on. If the element is present on the page but has style="display:none;", then isDisplayed() will return false. It returns true if the element is displayed and false if it is not. Advantage of this method is that it avoids parsing an elements style attribute. isDisplayed is used in cases where the element is present in DOM and you need to check whether it is displayed or not in the UI. It is never used to check whether an element is present in the DOM.
isDisplayed()
if(driver.findElement(By.xpath(xpath_itself)).isDisplayed() )
{
/**Do this*/
}
else
{
/**Do this*/
}
isVisible is a method of the old Selenium RC and isDisplayed is a method of Selenium 2.
An element can be rendered invisible by setting the CSS visibility property to hidden or the display property to none, either for the element itself or one if its ancestors. This method will fail if the element is not present.
WebDriver's WebElement contains only the isDisplayed() method:
-
isDisplayed(): Is this element displayed or not? This method avoids the problem of having to parse an element'sstyleattribute. Before Webdriver, there was Selenium RC, which is now long deprecated. TheDefaultSeleniumclass had theisVisible()method. -
isVisible(): Determines if the specified element is visible. An element can be rendered invisible by setting the CSSvisibilityproperty tohiddenor thedisplayproperty tonone, either for the element itself or one if its ancestors. This method will fail if the element is not present. It looks for thedisplay: nonestyle tag, which might throw a null pointer if we aren't careful.
isElementPresent() method. Then try checking if the element is visible.
The visibility of an element is guided by what is perceptually visible to the human eye. In this context, an element’s displayedness does not relate to the visibility or display style properties.
The approach recommended to implementors to ascertain an element’s visibility was originally developed by the Selenium project and is based on crude approximations about an element's nature and relationship in the tree. An element is in general to be considered visible if any part of it is drawn on the canvas within the boundaries of the viewport.