Handling Checkbox

In this section, you will learn how to handle checkbox in selenium webdriver.

Let's create a test case in which we will automate the following scenarios:

  • Invoke a Google chrome browser.
  • Navigate to the website in which you handle the checkbox.
  • Select the 'Senior Citizen' checkbox from the spicejet website.
  • Close the driver.

Now, we will create a test case step by step in order to provide you proper understanding of how to handle checkbox.

Step 1: Launch the Eclipse IDE.

Step 2: Right click on the src folder and then click on the New > class.

Handling Checkbox
  • Enter the class name. I provide the class name as Checkbox_test.
Handling Checkbox

Step 3: Now, we will invoke the Google Chrome browser. We will download the chromedriver.exe file and set the system property to the path of your chromedriver.exe file.

Here is the sample code to set the system property to the path of a chromedriver.exe file.

snippet
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe);

Here is the sample code to invoke the Google chrome browser.

snippet
WebDriver driver = new ChromeDriver();

Combining both of the above code blocks, we will get the code snippet to launch Google chrome browser.

snippet
// Set the system property
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe);

// Launch the Google Chrome browser.
WebDriver driver = new ChromeDriver();

Step 4: We are done with the first test case, i.e., invoking a Google chrome browser. Now we will write the code to automate the test case. Our second test case is to navigate to the "spicejet" website.

Here is the sample code to navigate to the "spicejet" website.

snippet
driver.navigate().to("https://www.spicejet.com/");

Here is the complete code:

snippet
package mypack;

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

public class Checkbox_test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.navigate().to("https://www.spicejet.com/");

	}}

Step 5: Now we try to locate the 'Senior Citizen' checkbox by inspecting its HTML code.

Handling Checkbox

Note the id attribute of a checkbox.

Handling Checkbox

In the above case, we observe that 'id' is a unique attribute, so we locate the checkbox by using an id attribute.

Step 6: To automate the third test case, we need to write the code that will locate 'Senior Citizen' checkbox.

Here is the code that will handle the "Senior Citizen" checkbox.

snippet
package mypack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test 
{

public static void main(String[] args) 
{
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.navigate().to("https://www.spicejet.com/");
		System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
		driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();
		System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());

driver.close();
	}

}

In the above code, we haven't used the complete 'id' attribute value as it is very big. I have used the half part of the 'Senior Citizen' checkbox, and other half part is represented in the form of regular expression, i.e., '*='.

We have used two methods in the above code:

  • isSelected(): This method determines whether the checkbox is selected or not. If the checkbox is selected, then this method returns true otherwise false.
  • click(): This method selects the locator. In this case, it is selecting the "Senior Citizen" checkbox.

Output

Handling Checkbox

Output on the Console

Handling Checkbox
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents