Automation Using Selenium Webdriver

Wednesday 4 January 2017

How to Automate Radio button and Checkbox in Selenium webdriver

How to Automate Radio button and Checkbox in Selenium webdriver


                      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.

Let us get started

Demo Code


WebElement ele=driver.findElement(By.id());
ele.click();

WebElement ele=driver.findElement(By.id());
ele.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. You will get these type of questions in interviews also.

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

isSelected();
Therefore, you must be eager now how to use these methods in script so let us see these methods using a single program.



Program to Automate radio button and checkbox in selenium

 import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FacebookDropdown {

     public static void main(String[] args) throws Exception {

          WebDriver driver=new FirefoxDriver();

          driver.manage().window().maximize();

          driver.get("http://www.facebook.com");

         WebElement male_radio_button=driver.findElement(By.id("u_0_e"));

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

     }

}


 import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FacebookDropdown {

     public static void main(String[] args) throws Exception {

          WebDriver driver=new FirefoxDriver();

          driver.manage().window().maximize();

          driver.get("http://www.facebook.com");

         WebElement male_radio_button=driver.findElement(By.id("u_0_e"));

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

     }

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

Output:
Male radio button is display>>>true
Male radio button is Enable>>true
Male radio button is selected>>false
Click on radio button
Male radio button is selected>>True


     

1 comment: