Automate radio button and checkbox in selenium

Hello Guys, Welcome to QA Earth's Selenium tutorial in this post we will see how to automate radio button and checkbox in selenium

Before moving to radio button and checkbox, you should be familiar with HTML as well.

Before moving to Checkbox please refer Basic Selenium Tutorial

HTML For Checkbox
<input type=”checkbox”>
For Radio Button
<input type=”radio”> 



Automate radio button and checkbox in selenium

When you inspect these elements via firebug and firepath you will get above html type.

The main difference between radio button and checkbox is checkbox you can select multiple but for radio button, only one selection is possible.

In Selenium we have 1 method called click() to perform click events.

This click() method you can apply with radio button, checkbox, links and sometime with dropdown as well.
WebElement ele=driver.findElement(By.id()).click();
In this example I have used id only but if you want to make you script stable then you should use Xpath and CSS in your script.

Before performing click action, sometimes we need to verify some activity as well, take some example
  • You need to verify whether radio button or checkbox is enabled.
  • You need to verify whether radio button or checkbox is Displayed on UI or not.
  • You need to verify whether checkbox and radio button is default selected or not.
Above validations are must used in script because automation is all about validation only.

These words looks quite big while listening but we can easily verify this using some predefined method in Selenium.

These methods are
isDisplayed();
isEnabled();
isSelected();
Program to Automate radio button and checkbox in selenium
package com.blog.qaearth.acc;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class QAEarthRadioButton {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://qaearth.blogspot.in/p/testing-examples.html");
WebElement male_radio_button = driver.findElement(By.id("radio1"));
boolean status = male_radio_button.isDisplayed();
System.out.println("Male radio button is Displayed >>" + status);
boolean enabled_status = male_radio_button.isEnabled();
System.out.println("Male radio button is Enabled >>" + enabled_status);
boolean selected_status = male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>" + selected_status);
male_radio_button.click();
boolean selected_status_new = male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>" + selected_status_new);
}

If you notice above scenario before click Selected status was false but after click status changed to TRUE.

Check below image for output.


Once you get the status you can easily verify using Assert in TestNG.

Above example will work for Checkbox as well. Please try from your side and let me know if you help required.