Writing automation script for check boxes and Radio Buttons

Writing automation script for check boxes and Radio Buttons

In this tutorial, we are going to learn about – Writing Automation Script for check boxes and Radio buttons. We are going to use Selenium v3 in this tutorial.

This tutorial is part of complete Selenium 3.0 Tutorials, being developed by Techcanvass.

We are going to cover the following topics:

  • Methods to interact with Check boxes
  • Methods to interact with Radio Buttons
  • Writing the test cases for both

Locating Check boxes and Radio Buttons

We can locate the check boxes and radio buttons on a web page using any of the locators or as per the locators usage strategy for the project.

You can refer to the detailed post on locators in our tutorial series.

Basics of Locators Selenium

Basics of Locators in Selenium

Interacting with Check boxes and Radio buttons

Click() Method

Click method is used to select a value in checkbox or radio buttons. Once we locate the radio button or check box, we can use the click() method to select a value.

driver.findElement(By.id(“checkbox1”)).click();

IsSelected()

isSelected() method is used to ascertain if the check box or radio button is already selected? The example code written below shows the usage:

if ( !driver.findElement(By.id(“checkbox1”)).isSelected() )

{

driver.findElement(By.id(“checkbox1”)).click();

}

In the above code, we first check if the checkbox is selected, if not, then we select the check box.

Example for this tutorial

We are going to use http://techcanvass.com/examples/register.html for this example. This is a simple web page to register early for buying a smartphone to avail heavy discounts.

Register Page for Selenium

Writing Script for Radio Button

If you inspect the code for radio button, you will realize that the names for both the radio buttons is the same as “gender”, only the values differ. This is possible if IDs are not provided for the radio buttons or bad HTML coding practice.

Using XPath as the locator strategy, will serve well. Use Firepath tool to get the locators for both the radio buttons and create web elements as shown below:

WebElement radio1 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[1]”));
WebElement radio2 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[2]”));

Writing Script for Check Box

We can use the XPath strategy for check boxes also.

Complete code

In our case, we will be using the following test data to submit the registration for the smart phone:

First Name: Meenaxi

Last Name: Kabra

Gender: Female

Model: Mega 123 large screen (Default value so we will not change)

Should it be delivered to your house: YES

We have talked about text boxes in the previous tutorial. You can click on the image below to access that tutorial:

Handling Text Boxes in Selenium 3

Below is the complete code for the web page, we have not entered anything for the address fields.

package basicSeleniumScripts;
import org.openqa.selenium.By;

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

public class CheckboxRadiobutton {

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

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);
WebDriver driver;
driver = new ChromeDriver();

driver.get(“http://techcanvass.com/examples/register.html”);

WebElement TxtFname = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[1]/input[1]”));

WebElement TxtLname = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[1]/input[2]”));

WebElement radio1 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[1]”));
WebElement radio2 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[2]”));

TxtFname.sendKeys(“Meenaxi”);
TxtLname.sendKeys(“Kabra”);

if ( radio1.isSelected())
{
Thread.sleep(5000);
radio2.click();
Thread.sleep(10000);

}

if ( radio2.isSelected())
{
radio1.click();
Thread.sleep(10000);

}

WebElement Checkbx = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[6]/input”));

Checkbx.click();
Thread.sleep(10000);

driver.quit();

}

}

Please note that we have used Thread.Sleep() only to demonstrate that values have been entered into the fields.

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course

Selenium Training with Java

Selenium Training Course

Cheers

Leave a Reply

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