C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Handling CheckboxIn 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:
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.
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. Note the id attribute of a 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:
Output Output on the Console
Next TopicAssertions
|