PAGE 8 : HOW TO WORK WITH CHECK BOX AND RADIO BUTTON IN SELENIUM WEBDRIVER

Check box and Radio button operations is very simple to perform like as click operation. Checkbox is always recommended to check if that is already in selected mode or deselected. Because, if it is already selected and when you click on the same element it will get deselected. Same case with the radio button too.







Let me explain you with an example:

package com.checkbox;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class CheckBox {

@Test
public void selectCheckBox() {

WebDriver myTestDriver = new FirefoxDriver();
myTestDriver.get("https://www.facebook.com/");


//Here we will be finding all the available check box with id as "persist_box" and will put it in a list
List<WebElement> CHECKBOXlist = myTestDriver.findElements(By.id("persist_box"));



//Here we will be verifying whether the check box with id as "persist_box" is selected or not
for (int i = 0; i < CHECKBOXlist.size(); i++) {
System.out.println(CHECKBOXlist.get(i).isSelected());
}


//With the help of click method we will be clicking (check) on to the check box
for (int i = 0; i < CHECKBOXlist.size(); i++) {
CHECKBOXlist.get(i).click();
}

//Now we will verify whether the check box is selected or not
for (int i = 0; i < CHECKBOXlist.size(); i++) {
System.out.println(CHECKBOXlist.get(i).isSelected());
}
}

}


Executing test via TestNg.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Select Check Box ">
<test name="All Operations on Check box">
<classes>
<class name="com.checkbox.CheckBox" />
</classes>
</test>
</suite>


Output:
false -> Initially the check box was unchecked
true -> Now the check box is checked


RADIO BUTTON:







Let me explain you with an example:

package com.checkbox;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class RadioButton {

WebDriver driver;

@Test
public void selectRadioButton() {

driver = new FirefoxDriver();
driver.get("http://facebook.com");


//Here we will be finding the radio button on which we are going to perform operations
WebElement female_radio_button=driver.findElement(By.id("u_0_d"));

     
//To check whether the radio button is available
boolean status=female_radio_button.isDisplayed();

                System.out.println("Male radio button is Displayed >>"+status);
     
     
//To check whether the radio button is enabled
                boolean enabled_status=female_radio_button.isEnabled();

                System.out.println("Male radio button is Enabled >>"+enabled_status);
       
       
       
//To check whether the radio button is already checked or not
               boolean selected_status=female_radio_button.isSelected();

               System.out.println("Male radio button is Selected >>"+selected_status);
       
       
// To check the radio button
              female_radio_button.click();


// To verify whether the radio button is selected.
              boolean selected_status_new=female_radio_button.isSelected();

              System.out.println("Male radio button is Selected >>"+selected_status_new);
       
}

}


In case there are multiple radio buttons, then the below code can find the list of radio buttons and check if its unchecked.

Let me explain you with an example:

EXAMPLE:

package com.checkbox;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class RadioButton2 {

@Test
public void checkRadioButton() throws InterruptedException {

WebDriver myTestDriver = new FirefoxDriver();
myTestDriver.get("http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html");


//Below code will maximize the brower
myTestDriver.manage().window().maximize();


//Below code will find the radio buttons with name as "pizzasize" and put it in a list
List<WebElement> RadioGroup1 = myTestDriver.findElements(By.name("pizzasize"));

//Below code will count the number of Radio buttons
        System.out.println("Number of radio buttons are : " + RadioGroup1.size());


// Below code will check each  radio button whether its selected or not one by one


       for (int i = 0; i < RadioGroup1.size(); i++) {
    System.out.println("Before Clicking The Radio Button , Is selected : "
    + RadioGroup1.get(i).isSelected());


// If the radio button is not selected then it will select by clicking on to it
    RadioGroup1.get(i).click();

// The below code will check whether the check box is selected or not
    System.out.println("After Clicking The Radio Button , Is selected :   "
    + RadioGroup1.get(i).isSelected());

}

}

}



Executing tests via TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Select Radio Button">
<test name="All Operations on Radio Button">
<classes>
<class name="com.checkbox.RadioButton2" />
</classes>
</test>
</suite>


Output:
Number of radio buttons are : 6
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true
Before Clicking The Radio Button , Is selected :  : false
After Clicking The Radio Button , Is selected :   true

===============================================
Select Radio Button
Total tests run: 1, Failures: 0, Skips: 0
===============================================






No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com