Robot Framework is a generic open-source automation framework. It can be used for test automation and robotic process automation (RPA). RPA is extensively used for Web Application Automation, API Automation, RPA, and Database Testing.
Robot Framework has an easy syntax, utilizing human-readable keywords. Its capabilities can be extended by libraries implemented with Python, Java, or many other programming languages.
In this tutorial, will find out how the Radio Buttons can be automated in Selenium WebDriver. The radio Button is also a Web Element like Checkbox.
What is Radio Button?
A radio button is a form element in HTML that allows users to select one option from a predefined set of options. Radio buttons are typically used in scenarios where only one selection is allowed at a time, such as choosing a gender, selecting a subscription plan
The below image shows the Radio Buttons before they were selected.
Below is the image of the locators of this web element.
Let’s go through the scenario below:-
1) Launch Chrome Browser 2) Maximize the current window 3) Implicitly wait for 5 sec 4) Open browser – https://demo.automationtesting.in/Register.html 5) Find the locator of all Radio Buttons 6) Find the number of Radio Buttons available 7) Print the name of the first option of the radio button 8) Select the first option of the radio button 9) Print the name of the second option of the radio button 10) Select the second option of the radio button 11) Close the browser
Detailed Explanation
1.Launch Chrome Browser
A new instance of Chrome browser is created using the specified options.
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
This command finds all radio buttons on the page that have the attribute name =”radiooptions”. It returns a list of WebElement objects representing each radio button.
5. Get the Number of Radio Buttons
int radioSize = Radio_Options.size();
System.out.println("No Of Radio Button Options :" + radioSize);
6. Loop Through Radio Buttons
for (int i = 0; i < radioSize; i++) {
System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("Value"));
Radio_Options.get(i).click();
System.out.println("Radio Button Option "+ (i+1) +" is selected");
}
The script loops through each radio button in the list
7. Close the Browser
driver.quit();
The program for the above scenario is shown below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.time.Duration;
import java.util.List;
public class RadioButton_Demo {
public static void main(String[] args) throws InterruptedException {
// Initiate Chrome browser
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
// Maximize the browser
driver.manage().window().maximize();
// Put an Implicit wait and launch URL
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("https://demo.automationtesting.in/Register.html");
// Find locator of all Radio Buttons
List<WebElement> Radio_Options = driver.findElements(By.xpath("//*[@name='radiooptions']"));
// Find no of Radio Buttons available and print their values
int radioSize = Radio_Options.size();
System.out.println("No Of Radio Button Options :" + radioSize);
Thread.sleep(5000);
for (int i= 0; i< radioSize; i++) {
System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("Value"));
Radio_Options.get(i).click();
System.out.println("Radio Button Option "+ (i+1) +" is selected");
}
driver.quit() ;
}
}
The output of the above program is
We use By.xpath(“//*[@name=’radiooptions’] to locate the all the options of the radio button.
We use the click() method to select the radio button.