In this article we will learn how to identify following form elements and work with it with real examples.

  • Radio button
  • Check Box

Radio Button

Radio button can too be work with click() method of Selenium WebDriver. Basically radio buttons have the functionality to be clicked once and selection is only performed on single WebElement and not on multiple elements.

Let us understand Radio button with some example.

Test scenario to be automated
  • Open following URL: https://rahulshettyacademy.com/AutomationPractice/
  • Inspect WebElement for radio buttons
  • Radio1
  • Radio2
  • Radio3
  • Perform click event on Radio2.
  • Validate if radio button isSelected() using Selenium WebDriver methods.
  • Perform click event on Radio1.
  • Close WebDriver.

Below is java code for test scenario.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButton {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\vikra\\Downloads\\chromedriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");
        //Code to find Radio button with text Radio2 using XPath
        WebElement radio2=driver.findElement(By.xpath("//input[@value='radio2']"));
        radio2.click();

        //To validate if radio button is selected or not and performing click event on radio button.
        WebElement radio1 = driver.findElement(By.xpath("//input[@value='radio1']"));
        boolean selectState = radio1.isSelected();
        //Code to check if radio button is already selected
        if (!selectState){
            radio1.click();
        }
        //Close chrome driver
        driver.close();
    }
}

In the above code when driver comes to if condition the check will be made if the radio is selected or not, if selected condition will be false and driver will not go into if logic and test script will terminate indicating radio button is already selected. In this case since the radio button is not selected if condition is true driver moves into if logic and perform click operation on “Radio1”.

Code to verify isDisplayed() method of Selenium WebDriver. 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButton {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To validate if radio button is selected or not and performing click event on radio button.
        WebElement radio1 = driver.findElement(By.xpath("//input[@value='radio1']"));
        boolean selectState = radio1.isDisplayed();
       
        if (!selectState){
            radio1.click();
        }
        //Close chrome driver
        driver.close();
    }
}

The above test will validate if the given radio button displays on the page or not. If displayed, then it will select. The output of the above code will be the same as was in the case of “isSelected()“. Because the specified radio button is displayed, and it will be selected using the click option.

Code to verify isEnabled() method of Selenium WebDriver

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButton {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To validate if radio button is enabled or not and performing click event on radio button.
        WebElement radio1 = driver.findElement(By.xpath("//input[@value='radio1']"));
        boolean selectState = radio1.isEnabled();
       
        if (!selectState){
            radio1.click();
        }
        //Close chrome driver
        driver.close();
    }
}

The above code will first check if the element is in enabled status or not. If enabled, then it will perform the click operation. Else, no operation will perform. So, in this scenario, the radio button does not enable. Therefore, no click operation will perform on the specified radio button.

Check Box.

Check box is also selected using click() method of Selenium WebDriver, in this the driver check if the check box is selected or not and based on the condition perform selection operation on the check box.

Let us understand check box using some real example.

Test scenario to be automated

  • Open following URL: https://rahulshettyacademy.com/AutomationPractice/
  • Inspect WebElement for checkbox - 
  • Option1
  • Option2
  • Option3
  • Perform click event on Option3.
  • Validate if check box isSelected() using Selenium WebDriver methods.
  • Perform click event on Option1.
  • Close WebDriver.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBox {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\vikra\\Downloads\\chromedriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To find web element using Xpath and performing click event on check box
        WebElement option3=driver.findElement(By.xpath("//input[@value='option3']"));
        option3.click();
    }
}

How to perform validation on check box using Selenium WebDriver.

Selenium WebDriver provides certain methods that we can use for a pre and post validation of the states of a CheckBox. Few of these methods are:

  • isSelected(): Checks whether a checkbox is selected or not.
  • isDisplayed(): Checks whether a checkbox displays on the web page or not.
  • isEnabled(): Checks whether a checkbox is enabled or not.
How to use the isSelected() method to validate if the check box is selected?

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBox {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To find web element using Xpath and performing click event on check box
        WebElement option3=driver.findElement(By.xpath("//input[@value='option3']"));
        boolean isSelected = option3.isSelected();

        if (!isSelected){
            option3.click();
        }
    }
}

Once we ran this code, the code will first check whether the checkbox or not. Then an if condition will validate if the returned value is true or false. In case it’s false, i.e., the checkbox will appear unchecked. The code inside the if condition will execute, and the checkbox will check.

How to use the isDisplayed() method to validate if the check box is displayed?

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBox {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get("https://rahulshettyacademy.com/AutomationPractice/");

//To find web element using Xpath and performing click event on check box
WebElement option3=driver.findElement(By.xpath("//input[@value='option3']"));
boolean isSelected = option3.isDisplayed();

if (!isSelected){
option3.click();
}
}
}

The above test will validate if the given checkbox displays on the page or not. If it is displayed, then it will make a selection. The output of the above code will be the same as was in the case of “isSelected()” as the specified checkbox is displayed, and it will check using the click option.

How to use the isEnabled() method to validate if the check box is enabled?


package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBox {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To find web element using Xpath and performing click event on check box
        WebElement option3=driver.findElement(By.xpath("//input[@value='option3']"));
        boolean isSelected = option3.isEnabled();

        if (!isSelected){
            option3.click();
        }
    }
}

The above code will first check if the element is in an enabled state or not; if enabled, it will perform the click operation. If disabled, it will not perform any operation.

Let us now check how we can perform click on all check box using for loop condition.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class CheckBox {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //To find web element using Xpath and performing click event on check box
        List<WebElement> checkboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));

        for(int i = 0 ; i < checkboxes.size(); i++){
            checkboxes.get(i).click();
        }
        //Close chrome driver
        driver.close();
    }
}

Summary

The table below summarizes the commands to access each type of element discussed above.

Element

Command

Description

Check Box, Radio Button

click()

Used to toggle the element on/off


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}