Discover how to inspect the visibility of web elements with an assortment of loop and conditional commands in WebDriver:
In the prior guide, we went through the Select class in WebDriver, primarily implemented to manage dropdowns and choose different options within them.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Continuing with our Selenium guide series, this tutorial will explore different loop and conditional commands in WebDriver, such as isSelected(), isEnabled(), and isDisplayed(). These methods are utilized to establish the visibility range of web elements.
We commence with a succinct introduction. WebDriver adheres to a W3C specification that delivers information about the visibility preferences of disparate types of web elements upon which actions are executed.
WebDriver offers these methods to inspect web element visibility, such as buttons, dropdowns, checkboxes, radio buttons, and labels:
- isDisplayed()
- isSelected()
- isEnabled()
To better comprehend these methods, let’s inspect them through coding illustrations.
We will use “google.com” as the application undergoing the test and the “Learning_Selenium” project established in previous guides for script crafting.
Automation Scenario:
- Initiate the web browser and launch the application undergoing the test – http://google.com
- Authenticate the web page title
- Inspect if the “Google Search” button is displayed
- Input a keyword into the “Google Search” text box
- Inspect if the “Search” button is visible and activated
- If the “Search” button is in sight, click on it
What You Will Learn:
WebDriver Code
Step 1: Craft a new Java class labeled “VisibilityConditions” inside the “Learning_Selenium” project.
Step 2: Duplicate and paste the following code inside the “VisibilityConditions.java” class.
Here is the test script akin to the scenario described above:
<
pre class=”brush: java; title: ; notranslate” title=””> import org.openqa.selenium.By;
Description of the Code
Here are methods to authenticate the existence of web elements on a webpage:
boolean submitButtonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isDispalyed()
isDisplayed() is a method applied to authenticate a web element’s presence on a webpage. It yields a Boolean value – true if the specified web element is present and noticeable, and false if the web element is absent from the webpage.
In the code snippet above, we are using isDisplayed() to inspect if the “Google Search” button is present on the Google webpage. If observable, it will return true; otherwise, it will yield false.
boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
isEnabled()
isEnabled() is a method utilized to authenticate if a web element is enabled or deactivated on a webpage. Like isDisplayed(), it yields a Boolean value – true if the specified web element is enabled, and false if the web element is disabled.
The code snippet above inspects if the “Search” button is enabled or disabled and returns a Boolean value based on the result.
isEnabled() is often utilized with buttons and other interactive web elements.
isSelected()
isSelected() is a method utilized to authenticate if a web element is selected or not. It is primarily used with radio buttons, dropdowns, and checkboxes. It also returns a Boolean value – true if the specified web element is selected, and false if unselected.
In the code snippet above, we are inspecting if the male radio button is selected or not, and the method yields a Boolean value accordingly.
Conclusion
In this guide, we delved into the looping and conditional commands in WebDriver. These conditional methods cover nearly all visibility options for web elements.
Summary:
- WebDriver adheres to a W3C specification that delivers information about the visibility preferences of varying types of web elements.
- isDisplayed() is utilized to authenticate a web element’s presence on a webpage. It returns true if the web element is present and false if absent.
- isDisplayed() can authenticate the presence of any type of web element.
- isEnabled() is utilized to authenticate if a web element is enabled or disabled on a webpage.
- isEnabled() is typically used with buttons.
- isSelected() is utilized to authenticate if a web element is selected or not. It is primarily used with radio buttons, dropdowns, and checkboxes.
Upcoming Tutorial #15: When interacting with web apps, we frequently come across time delays when the webpage reloads or fresh web elements are loaded. This delay might stem from ajax calls or other factors. In the subsequent guide, we will study how to handle such time lags using implicit and explicit waits.
Reader Note: In the meantime, you can automate and test web element visibility utilizing the methods offered by WebDriver.