Check the visibility of web components with the WebDriver commands

Tram Ho

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:

Code Walkthrough

Here are ways to determine the visibility of web elements on a web page.

isDispalyed ()

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 ()

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 ()

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/

Share the news now

Source : Viblo