Welcome back to Selenium Webdriver free training. In the last tutorial, we talked about Multi-Browser testing. By performing Multi Browser testing, you can test your web app on different browsers.
Let’s move ahead and discuss how to select the checkbox and radio button.
What are the checkbox and radio button?
The checkbox and radio button are both web elements. You can find both in different forms like registration, or any agreement forms.
Let’s take an example of http://training.qaonlinetraining.com/testPage.php where you will find a radio button under the “Gender” section and checkboxes under the preference section “what do you have”. Gender under http://training.qaonlinetraining.com/testPage.php is required, which means if we choose gender then we should not get an error, otherwise, it will give an error like “Gender is required.”
Selenium Webdriver Script Actions
Here are the tasks that we need to perform by selenium script.
1. Open the http://training.qaonlinetraining.com/testPage.php page.
2. Enter name.
3. Enter email.
4. Enter the website
5. Enter the comment.
6. Choose gender
7. Select multiple preferences
And click on the submit button.
Live script demo
So, here is the demo of what we are going to do:
Watch How to handle the Radio button using Selenium Webdriver
Webdriver Script to Handle Checkbox and Radio button
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) { System.setProperty("webdriver.chrome.driver", "c:\\xampp\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://training.qaonlinetraining.com/testPage.php"); driver.findElement(By.name("name")).sendKeys("DemoName"); System.out.println("name\")).sendKeys(\"DemoName"); // E mail section driver.findElement(By.name("email")).sendKeys("DemoEmail@demo.com"); System.out.println("email\")).sendKeys(\"DemoEmail@demo.com"); // Website section driver.findElement(By.name("website")).sendKeys("www.demosite.com"); System.out.println("website\")).sendKeys(\"www.demosite.com"); // Comment section driver.findElement(By.name("comment")).sendKeys("demo comment"); System.out.println("comment\")).sendKeys(\"demo comment"); // Select Gender as female WebElement radioButtonFemale = driver.findElement(By.xpath("//input[@value='female']")); radioButtonFemale.click(); WebElement checkboxBoat = driver.findElement(By.xpath("//input[@value='boat']")); // checkbox bike WebElement checkboxBike = driver.findElement(By.xpath("//input[@value='Bike']")); // checkbox horse WebElement checkboxHorse = driver.findElement(By.xpath("//input[@value='horse']")); // selecting all options except car- selected by-default checkboxBoat.click(); checkboxBike.click(); checkboxHorse.click(); // Submit button driver.findElement(By.name("submit")).click(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } WebElement gendererr = driver.findElement(By.xpath("/html/body/form/span[4]")); String errtext = gendererr.getText(); if (errtext.contains("Gender is required")) { System.out.println("Got gender required error."); } else { System.out.println("Form functionality is working correctly."); } driver.close(); } }
Instructor-led Training
Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:
For Instructor-led training
Master of Functional Automation Testing
For Self-Driven training
Conclusion
So in this way, you can test checkboxes and radio buttons through the selenium webdriver script. In the next tutorial, we will talk about how to test a dropdown list with Selenium webdriver.