Different Selection Method by Id: Boxes and Radio Buttons. We Might Like To Check That If The Checkbox Is
Different Selection Method by Id: Boxes and Radio Buttons. We Might Like To Check That If The Checkbox Is
By ID
If ID is given for the Radio Button/CheckBox and you just want to click on it
irrespective of its value, then the command will be like this:
With IsSelected
If your choice is based on the pre-selection of the Radio
Button/CheckBox and you just need to select the deselected Radio
Button/CheckBox. Assume there are two Radio Buttons/Check Boxes, one is
selected by default and you want to select the other one for your test.
With IsSelected statement, you can get to know that the element is
selected or not.
With Value
You can even select Radio Buttons/Check Boxes with their Values.
By CssSelector
A simple way of selecting a check-box or radio button is by using its value:
Practice Exercise
1. Launch new Browser
2. Open “http://toolsqa.com/automation-practice-form/“
3. Challenge One – Select the deselected Radio button (female) for
category Sex (Use IsSelected method)
4. Challenge Two – Select the Third radio button for category ‘Years of
Exp’ (Use Id attribute to select Radio button)
5. Challenge Three – Check the CheckBox ‘Automation Tester’ for
category ‘Profession'( Use Value attribute to match the selection)
6. Challenge Four – Check the CheckBox ‘Selenium IDE’ for category
‘Automation Tool’ (Use cssSelector)
Solution
1 package automationFramework;
2
3 import java.util.List;
4 import java.util.concurrent.TimeUnit;
5
6 import org.openqa.selenium.By;
7 import org.openqa.selenium.WebDriver;
8 import org.openqa.selenium.WebElement;
9 import org.openqa.selenium.firefox.FirefoxDriver;
10
11 public class CheckBxRadioBtn {
12
13 public static void main(String[] args) {
14 // Create a new instance of the FireFox driver
15 WebDriver driver = new FirefoxDriver();
16
17 // Put an Implicit wait,
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19
20
21 // Launch the URL
22 driver.get("http://toolsqa.com/automation-practice-form");
23
24 // Step 3 : Select the deselected Radio button (female) for category Sex (Use IsSelected method)
25 // Storing all the elements under category 'Sex' in the list of WebLements
26 List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex"));
27
28 // Create a boolean variable which will hold the value (True/False)
29 boolean bValue = false;
30
31
// This statement will return True, in case of first Radio button is selected
32
bValue = rdBtn_Sex.get(0).isSelected();
33
34
35 // This will check that if the bValue is True means if the first radio button is selected
36 if(bValue == true){
37 // This will select Second radio button, if the first radio button is selected by default
38 rdBtn_Sex.get(1).click();
39 }else{
40 // If the first radio button is not selected by default, the first will be selected
41 rdBtn_Sex.get(0).click();
42 }
43
44 //Step 4: Select the Third radio button for category 'Years of Exp' (Use Id attribute to select Radio button)
45 WebElement rdBtn_Exp = driver.findElement(By.id("exp-2"));
46 rdBtn_Exp.click();
47
48 // STep 5: Check the Check Box 'Automation Tester' for category 'Profession'( Use Value attribute to match the selection)
49 // Find the Check Box or radio button element by Name
50 List<WebElement> chkBx_Profession = driver.findElements(By.name("profession"));
51
52
53 // This will tell you the number of Check Boxes are present
54 int iSize = chkBx_Profession.size();
55
56 // Start the loop from first Check Box to last Check Boxe
57 for(int i=0; i < iSize ; i++ ){
58 // Store the Check Box name to the string variable, using 'Value' attribute
59 String sValue = chkBx_Profession.get(i).getAttribute("value");
60
61 // Select the Check Box it the value of the Check Box is same what you are looking for
62 if (sValue.equalsIgnoreCase("Automation Tester")){
63 chkBx_Profession.get(i).click();
64 // This will take the execution out of for loop
65 break;
66 }
}
// Step 6: Check the Check Box 'Selenium IDE' for category 'Automation Tool' (Use cssSelector)
67 WebElement oCheck Box = driver.findElement(By.cssSelector("input[value='Selenium IDE']"));
68 oCheck Box.click();
69
70
71 // Kill the browser
72 driver.quit();
73
74 }
}
Category: Selenium-Webdriver