How can I validate a web element in Selenium?
The tutorials covered till now were focused mainly on how to automate different scenarios. In this tutorial, we will cover how to validate whether the results are as expected or not.
Validation means checking whether the UI elements are rendered on the web page correctly or not.
For Example:
- Checking whether the hidden elements are hidden or not.
- Checking whether the form input field is editable or not.
It’s very important to put checks and validation at each and every point possible to write good automation scripts.
Every application under test has lots of validations involved during the “Testing process”. Selenium online training provides various methods to validate UI elements.
Type of UI Validations:
- Page Title Validation
- Page URL Validation
- Scroll Down
- Verifying hidden elements
- Checkbox Validations
- Radio button Validations
- To verify whether a dropdown allows multiple selections or not.
- Read-only property of WebElement.
- Check if an Element exists or not.
- Textbox value validation
- Disabled/Enabled property of a Web Element
- Page Content Validations
Let us learn each type of validation one-by-one:
Page Title Validation: To verify the title of the page, we have a method called getTitle().
//To get the title of the page
Driver.getTitle();
Page URL Validation:
To verify the URL of the page, there is a method called getCurrentUrl().
//To get the current url of the page
Driver.getCurrentUrl();
Scroll Down Operation: There are some scenarios where web elements render on a page once you scroll down. Scroll Down operation in Selenium WebDriver can be performed by invoking JavaScript. We have covered a this in a separate tutorial.
Hidden Element Verification: To verify whether a Web Element is visible or hidden on the page. Selenium has a method called isDisplayed(). It’s a boolean method which returns true if the WebElement is visible and return false if the method is hidden.
boolean status = Driver.findElement(By.xpath("//input[@id='radioBtn']")).isDisplayed();
if(status){
System.out.println("Element is visible");
} else {
System.out.println("Checkbox is hidden");
}
Checkbox Validation:
To verify that a checkbox is checked or not. Best Selenium course online provides a method called isEnabled(). It’s a boolean method which returns true if the checkbox is checked and returns false if it is not checked.
boolean status = Driver.findElement(By.xpath("//input[@type='checkbox']")).isEnabled();
if(status){
System.out.println("Checkbox is checked");
} else {
System.out.println("Checkbox is unchecked");
}
Radio Button Validation:
To verify that a radio button is selected or not. Selenium provides a method called isSelected(). It’s a boolean method which returns true if it is enabled otherwise it returns false.
boolean status = Driver.findElement(By.xpath("//input[@type='radio']")).isSelected();
if(status){
System.out.println("Checkbox is checked");
} else {
System.out.println("Checkbox is unchecked");
}
To verify whether a
dropdown
allows multiple selections or not. There is a method called isMultiple(). This method is a boolean method which returns true if the dropdown allows multiple selections else will return false.
Select dropdown = new Select(Driver.findElement(By.id("searchList")));
boolean status = dropdown.isMultiple();
if(status){
System.out.println("Allows multiple selection");
} else {
System.out.println("Does not allow multiple selection");
}
isSelected, isMultiple and isDisplayed are three methods which can also be used to verify page contents, to verify whether a WebElement is enabled or greyed out, etc.