Automating radio buttons and radio groups

The Selenium WebDriver supports radio buttons and radio group elements using the WebElement interface. We can select and deselect the radio buttons using the click() method of the WebElement class, and check whether a radio button is selected or deselected using the isSelected() method.

In this recipe, we will see how to work with the radio button and radio group controls.

How to do it...

Let's create a test that will have radio buttons and the radio group controls. We will perform select and deselect operations, the following code shows an example:

@Test
public void testRadioButton() {
  // Get the Radio Button as WebElement using it's value attribute
  WebElement petrol = driver.findElement(By
      .xpath("//input[@value='Petrol']"));

  // Check if its already selected? otherwise select the Radio Button
  // by calling click() method
  if (!petrol.isSelected()) {
    petrol.click();
  }

  // Verify Radio Button is selected
  assertTrue(petrol.isSelected());

  // We can also get all the Radio buttons from a Radio Group in a list
  // using findElements() method along with Radio Group identifier
  List<WebElement> fuelType = driver.findElements(By.name("fuel_type"));
  for (WebElement type : fuelType) {
    // Search for Diesel Radio Button in the Radio Group and select it
    if (type.getAttribute("value").equals("Diesel")) {
      if (!type.isSelected()) {
        type.click();
      }
      assertTrue(type.isSelected());
      break;
    }
  }
}

How it works...

We can locate a radio button similar to any other element. In this example, XPath is used to locate the radio button using its value attribute:

//Get the Radiobutton as WebElement using it's value attribute
WebElement petrol = driver.findElement(By.xpath("//input[@value='Petrol']"));

We can select or deselect a radio button by using the click() method. There are no separate methods to perform these operations. When we want to select a radio button, we need to be careful and see to it that it's not already selected, or else, calling the click() method will deselect the radio button. We can check if a radio button is already selected by calling the isSelected() method, which returns true if it's selected and false if it's not selected. In the following example, the click() method will be called only when the radio button is not selected:

//Check if its already selected? otherwise select the Radiobutton
//by calling click() method
if (!petrol.isSelected()) {
  petrol.click();
}

Working with radio groups

Instead of just locating a single radio button, we can also work with a group of radio buttons by locating all the radio buttons from a group shown as a list of WebElement using the findElements() method. In the following example, a radio button from fuel_type radio group is retrieved in a list of WebElement:

//We can also get all the Radiobuttons from a Radio Group in a //list using findElements() method along with Radio Group
//identifier
List<WebElement> fuelType = driver.findElements(By.name("fuel_type"));

We can then iterate through this list to find a specific radio button and select or deselect a radio button as shown in the following example:

for (WebElement type : fuelType)
{
  //Search for Diesel Radiobutton in the Radio Group and select it
  if(type.getAttribute("value").equals("Diesel"))
  {
    if(!type.isSelected()) {
      type.click();
}

    assertTrue(type.isSelected());
    break;
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.60