The WebDriver has a W3C parameter that details information about different display options based on the type of web element the actions will be performed. WebDriver facilitates users to use the following methods to check the visibility of web components. These web components can be: buttons, drop boxes, checkboxes, radio buttons, label …
- isDisplayed ()
- isSelected ()
- isEnabled ()
To better understand, we will dive into the analysis with the code below. We will use the ” google.com ” page as a test application and use the “Learning_Selenium” project created in the previous instructions to create the script.
Automated test script:
- Launch the web browser and open the experimental application – http://google.com
- Verify site title
- Verify if the “Google Search” button is displayed or not
- Enter keywords in the “Google Search” text box
- Verify that the “Search button” button is displayed and enabled.
- Based on the visibility of the Search button, click on this search button
WebDriver Code
Step 1: Create a new java class named “VisibilityConditions” inside the “Learning_Selenium” project.
Step 2: Copy and paste the following code into the “VisibilityConditions.java” class. This is the code equivalent of the scenario mentioned above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class VisibilityConditions { /** * @param args */ public static void main(String[] args) { // objects and variables instantiation WebDriver driver = new FirefoxDriver(); String appUrl = "https://google.com"; // launch the firefox browser and open the application url driver.get(appUrl); // maximize the browser window driver.manage().window().maximize(); // declare and initialize the variable to store the expected title of the webpage. String expectedTitle = "Google"; // fetch the title of the web page and save it into a string variable String actualTitle = driver.getTitle(); // compare the expected title of the page with the actual title of the page and print the result if (expectedTitle.equals(actualTitle)) { System.out.println("Verification Successful - The correct title is displayed on the web page."); } else { System.out.println("Verification Failed - An incorrect title is displayed on the web page."); } // verify if the “Google Search” button is displayed and print the result boolean submitbuttonPresence=driver.findElement(By.id("gbqfba")).isDisplayed(); System.out.println(submitbuttonPresence); // enter the keyword in the “Google Search” text box by which we would want to make the request WebElement searchTextBox = driver.findElement(By.id("gbqfq")); searchTextBox.clear(); searchTextBox.sendKeys("Selenium"); // verify that the “Search button” is displayed and enabled boolean searchIconPresence = driver.findElement(By.id("gbqfb")).isDisplayed(); boolean searchIconEnabled = driver.findElement(By.id("gbqfb")).isEnabled(); if (searchIconPresence==true && searchIconEnabled==true) { // click on the search button WebElement searchIcon = driver.findElement(By.id("gbqfb")); searchIcon.click(); } // close the web browser driver.close(); System.out.println("Test script executed successfully."); // terminate the program System.exit(0); } } |
Code Walkthrough
Here are ways to determine the visibility of web elements on a web page.
isDispalyed ()
1 2 | boolean submitbuttonPresence=driver.findElement(By.id(“gbqfba”)).isDisplayed(); |
isDispalyed()
is the method used to verify the presence of a web component in a web page. The method designed to return a Boolean value (true || false) for each success or failure. This method returns true
if the specified web element is visible on the web page and returns false
if the web element is not displayed on the web page. The above code verifies the presence of the submit
button with id = “gbqfba” on google website: returns true
if this submit button is displayed and returns false
if the submit button does not appear on the page google web.
This method relates to the visibility of all types of web components without limitation of any kind.
isEnabled ()
1 2 | boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled(); |
isEnabled()
is the method used to verify whether the web element is enabled (enabled) or disabled (disabled / disabled) in the web page. Like the isDisplayed()
method, this method is designed so that the result returned is a Boolean value (true || false) with each success or failure. This method returns true
if the web element is enabled on the site and returns false
if the web element is not enabled (is disabled) on the web page.
Therefore, the above code verifies whether the submit
button with id = “gbqfb” is enabled and returns a Boolean value depending on the result.
The isEnabled()
makes sense in situations where we want to specify that only when condition A is implemented, the element (specified node) is enabled. For example the case is very common as illustrated below:
The Register
button is only enabled (available) when the agreement … checkbox is selected.
To check if the element is currently selected (selected), we have the following method:
isSelected ()
1 2 | boolean searchIconSelected = driver.findElement(By.id(“male”)).isSelected(); |
isSelected()
is the method used to verify if the web element is being selected. It is often used to check radio buttons, dropdowns and checkboxes. Similar to the above two methods, this method is designed so that the result returned is a Boolean value (true || false) for each success or failure.
In this article, we got acquainted with the iterative methods and WebDriver conditions. These conditional methods handle most of the types of display options for web components.
Article translated from: https://www.softwaretestinghelp.com/webdriver-commands-selenium-tutorial-14/