TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium WebDriver Handling Checkbox

Selenium WebDriver Handling Checkbox with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

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.

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

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

WebDriver driver = new ChromeDriver();

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

// 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.

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

Here is the complete code:

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.

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
Next TopicAssertions




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf