Check box and Radio buttons

Purpose of this tutorial is to give you some basic idea about Check box and Radio buttons in Selenium.
Checkbox is basically used for selecting one or multiple values in a form. With the help of Checkbox, we can select the desired values as per our need.
For example:
We have created one html page where we can select single or multiple hobbies in the form of check boxes. See below image.
Here we are using one practice form http://hugelearning.com/testpracticeform1/ where we can check our code.

checkbox1

Code Section:

package testpackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CheckboxTest {

	@Test
	public void dropDownCheck() throws InterruptedException {

		System.setProperty("webdriver.chrome.driver",
				"C:/Users/Hemant/workspace/Project07Aug/testgitproject1/chromedriver_win32/chromedriver.exe");

		// Open chrome driver
		WebDriver driver = new ChromeDriver();

		// Open test practice form on browser
		driver.get("www.hugelearning.com/testpracticeform1/");

		// Select checkboxes for Drawing and Singing
		driver.findElement(By.name("Hobby_Drawing")).click();
		driver.findElement(By.name("Hobby_Dancing")).click();

		driver.close();
	}
}

Using Check box, we can either select single and multiple values or options while using Radio buttons, we can be toggled the values. It means we need to select a single value at a single moment. And once we select any value other selected value will be automatically unselected.

For example:
We have created one html page where we can select radio button for Gender. See below image.
Here we are using one practice form http://hugelearning.com/testpracticeform1/ where we can check our code.

radiobutton

Code Section:

package testpackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CheckboxTest {

	@Test
	public void dropDownCheck() throws InterruptedException {

		System.setProperty("webdriver.chrome.driver",
				"C:/Users/Hemant/workspace/Project07Aug/testgitproject1/chromedriver_win32/chromedriver.exe");

		// Open chrome driver
		WebDriver driver = new ChromeDriver();

		// Open test practice form on browser
		driver.get("www.hugelearning.com/testpracticeform1/");

		// Select radiobutton for Gender
		driver.findElement(By.xpath("//*[@name='Gender' and @value='Male']")).click();

		driver.close();
	}
}