Tuesday, 20 March 2018

How to Automate Radio button and Checkbox in Selenium webdriver

In this post we are going to learn about how to test checkboxes and radio buttons using selenium.Before jumping into that
let us see how the html code for checkbox and radio button looks like.
HTML For Checkbox
<input type=”checkbox”>
HTML For Radio Button
<input type=”radio”>

The main difference between radio button and checkbox is checkbox you can select multiple but for radio button, only one selection is possible.
we can perform click action using selenium click() method but before that it is very important to verify / test some conditions listed below-

  •     isEnabled()-You need to verify whether radio button or checkbox is enabled.
  •     isDisplayed()-You need to verify whether radio button or checkbox is Displayed on UI or not.
  •     isSelected()-You need to verify whether checkbox and radio button is default selected or not.
Video Tutorial - 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

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

public class RadioButtonTest {

WebDriver driver;

     public  void test() throws Exception {
 System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();


         WebElement female_radio_button=driver.findElement(By.id("u_0_b"));
         boolean status=female_radio_button.isDisplayed();
         System.out.println("Female radio button is Displayed >>"+status);
          boolean enabled_status=female_radio_button.isEnabled();
          System.out.println("Female radio button is Enabled >>"+enabled_status);
        boolean selected_status=female_radio_button.isSelected();
          System.out.println("Female radio button is Selected >>"+selected_status);
        boolean selected_status_new=female_radio_button.isSelected();
          System.out.println("Female radio button is Selected >>"+selected_status_new);


You can practice with same code for chekboxes. Do try and share your feedback, suggestions and questions in the comment section below.


Post a Comment