25Apr

Handling Radio buttons and Checkboxes using Selenium Webdriver

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options.

When we inspect a Radio button or a Checkbox, HTML code looks as below

Radiobutton

 

Checkbox

 

Using Click() method in Selenium we can perform the action on the Radio button and on Checkbox.

Example:

Webelement  maleRadioBtn = driver.findElement (By.id("gender-male"));

maleRadioBtn.click();

Before performing the click even on the Radio buttons or checkboxes we will have to verify

  • If Radio button or Checkbox is displayed on the webpage
  • If Radio button or Checkbox is enabled on the webpage
  • Check the default selection of the Radio button or Checkbox

Above mentioined verification can be done using predefined menthods in Selenium

  • isDisplayed()
  • isEnabled()
  • isSelected()

isDisplayed()

WebElement maleRadioBtn = driver.findElement (By.id("gender-male"));

maleRadioBtn.isDisplayed // this returns a Boolean value, if it returns true then said radio button is present on the webpage or it returns False.

isEnabled()

maleRadioBtn.isEnabled() // this returns a Boolean value, if it returns true then said radio button is enabled on the webpage or it returns False

isSelected()

maleRadioBtn.isSelected() // this returns a Boolean value, if it returns true then said radio button is selected or it returns False

 

NOTE: The same menthods can be used while working with Checkboxes.

 

Below is the snippet of the code for Radio buttons

package com.explorations.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlingRadioButton

{

       public static void main (String args[])

       {

              FirefoxDriver driver = new FirefoxDriver();

              driver.get("http://sites.ecanarys.com/Nopcommercesite/");

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

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

             

              //Clicking on the Register link on the Home page

              driver.findElement(By.linkText("Register")).click();

             

              //Identifying Male radio button using its ID as an locator

              WebElement maleRadioBtn = driver.findElement(By.id("gender-male"));

             

              //Checking if the Male Radio button is displayed on the Webpage and printing the status

              boolean radioBtnIsDisplayed = maleRadioBtn.isDisplayed();

              System.out.println("Is Male radio button displayed: "+radioBtnIsDisplayed);

             

              //Checking if the Male Radio button is enabled on the webpage and printing the status

              boolean radioBtnIsEnabled = maleRadioBtn.isEnabled();

              System.out.println("Is Male radio button enabled: "+radioBtnIsEnabled);

             

              //Checking the default radio button selection status

              boolean radioBtnIsSelected = maleRadioBtn.isSelected();

              System.out.println("Default Radio button selection Status: "+radioBtnIsSelected);

             

              //Selecting male radio button

              maleRadioBtn.click();

             

              //rechecking the male radio button selection status and printing it..

              boolean radioBtnNewSelectionStatus = maleRadioBtn.isSelected();

              System.out.println("Male radio Selection status after perform click() event: "+radioBtnNewSelectionStatus);

              driver.quit();

       }

}

Radiobutton output

 

 

Below is the snippet of the code for Checkboxes

package com.explorations.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class HandlingCheckbox {

       public static void main(String args[])

       {

              FirefoxDriver driver = new FirefoxDriver();

              driver.get("http://sites.ecanarys.com/Nopcommercesite");

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

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

             

              //Clicking on the Register link on the Home page

              driver.findElement(By.linkText("Register")).click();

             

              //Identifying Terms and Condition checkbox using its ID as an locator

              WebElement tcCheckbox = driver.findElement(By.id("accept-privacy-policy"));

             

              //Checking if the Terms and Condition checkbox is displayed on the Webpage and printing its status

              boolean tcCheckIsDisplayed = tcCheckbox.isDisplayed();

              System.out.println("Is Terms and condition checkbox displayed: "+tcCheckIsDisplayed);

             

              //Checking if the Terms and Condition checkbox is enabled on the webpage and printing its status

              boolean tcCheckboxIsEnabled = tcCheckbox.isEnabled();

              System.out.println("Is Terms and condition checkbox enabled: "+tcCheckboxIsEnabled);

             

              //Checking the default selection status of Terms and Condition checkbox

              boolean tcCheckboxIsSelected = tcCheckbox.isSelected();

              System.out.println("Default Terms and Condition checkbox status: "+tcCheckboxIsSelected);

             

              //Selecting the Terms and conditions checkbox

              tcCheckbox.click();

             

              //rechecking the Terms and Condition checkbox status and priting its status

              boolean tcCheckboxNewStatus = tcCheckbox.isSelected();

              System.out.println("Terms and Condition checkbox status after perform click() event: "+tcCheckboxNewStatus);

              driver.quit();

       }

}

 

Checkbox output

 

 

Related

What is Defect? Defect Life Cycle in Software Testing.

Defect is an unexpected behavior of the software application flow against the requirement specificat...

Read More >

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Assertions in Coded UI

Let us discuss how to add assertions in coded UI. Assertions are checkpoints/benchmarks to UI c...

Read More >

Analysis of Load Test Results

Analysis of Performance DataAfter you capture and consolidate your results, analyze the captured dat...

Read More >

Parallel Execution using Selenium Webdriver and TestNG

While testing a web application it is very important to test the application on different browsers. ...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *