In our previous posts, we covered topics on selenium tables, forms, waits and text fields. Now we will take a look at visibility conditions. Visibility conditions are necessary while running selenium tests, because they enable you check if an element is selected, displayed or enabled. For example, a tester may want to check if a check box has been selected on default before he/she clicks it. The best way to do this will be to use the selenium methods in the below.
- Boolean isSelected(): 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.
- Boolean isDisplayed(): This method determines if an element is displayed or not. 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.
- Boolean isEnabled(): 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.
For this tutorial we will be using our sample test page.
On our sample test page we have some check boxes, radio buttons and a save button. We will be checking if this elements are enabled, displayed or selected on the sample test page.
Steps to follow to understand when an element is disabled or enabled.
- Navigate to the sample test page.
- Go to the Check boxes & Radio button section
- Click on the save button when no radio buttons (Java or PHP) has been selected (You will notice that no action occurs, because element is visible but not enabled)
- Click on any of the radio buttons “Java or PHP”.
- Click on the save button (An event is performed and you can see the success message)
Now lets use code examples to understand how to implement this visibility conditions.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testdiary_VisibleConditions {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";
driver.get(contactUrl);
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("seleniumbox")));
// Declaring a local variable of type "WebElement" to initialize the
// value of the webElement (seleniumCheckbox) and webElement
// (restCheckbox)
WebElement seleniumCheckbox = driver.findElement(By.id("seleniumbox"));
WebElement restCheckbox = driver.findElement(By.id("restapibox"));
// wait was covered in prvious tutorial
(new WebDriverWait(driver, 30)).until(ExpectedConditions
.elementToBeClickable(seleniumCheckbox));
// Declaring a local variable of type "Boolean" to initialize the
// value of the webElement (checkSelenium) and webElement (checkRestApi)
// Using the isSelected method we check if the check box is selected or
// not.
Boolean checkSelenium = seleniumCheckbox.isSelected();
Boolean checkRestApi = restCheckbox.isSelected();
// use an if condition to check if boolean returned false
// If false then it was not selected
// click and select the checkbox
if (checkSelenium == false) {
seleniumCheckbox.click();
System.out.println("Test has selected Selenium checkbox");
} else {
System.out.println("Selenium checkbox was selected on default");
}
// Repeat the process for checkRestAapi
if (checkRestApi == false) {
restCheckbox.click();
System.out.println("Test has selected Rest api checkbox");
} else {
System.out.println("Rest Api checkbox was selected on default");
}
// Check if Save button is displayed on the WebPage
WebElement saveButton = driver.findElement(By.id("demo"));
Boolean checkSaveIsDisplayed = saveButton.isDisplayed();
if (checkSaveIsDisplayed == true) {
System.out.println("save button is displayed");
}
// Check if Save button is not enabled on the WebPage
Boolean checkSaveIsEnabled = saveButton.isEnabled();
if (checkSaveIsEnabled == false) {
System.out.println("save button is not enabled");
}
// Click on a radio button then check if the save button is now enabled
WebElement javaRadioButton = driver.findElement(By.id("java1"));
(new WebDriverWait(driver, 30)).until(ExpectedConditions
.elementToBeClickable(javaRadioButton));
javaRadioButton.click();
// check if it is now enabled
checkSaveIsEnabled = saveButton.isEnabled();
if (checkSaveIsEnabled == true) {
System.out.println("save button is enabled");
}
driver.close();
System.exit(0);
}
}
In conclusion, we have looked at the different conditions (isDisplayed, isSelected, isEnabled) selenium provides and how we can use them effectively in our test.You can download or clone this test from the Testdiary Github.
Click: VisibilityConditions Happy Testing!!!
Recent Comments