0
0
Selenium Javatesting~15 mins

Radio button handling in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify selection of a radio button on a web form
Preconditions (2)
Step 1: Navigate to the web form page URL
Step 2: Locate the radio button with id 'gender_male'
Step 3: Click the radio button 'gender_male'
Step 4: Verify that the radio button 'gender_male' is selected
✅ Expected Result: The radio button with id 'gender_male' is selected after clicking
Automation Requirements - Selenium WebDriver with Java and JUnit 5
Assertions Needed:
Verify the radio button is selected after clicking
Best Practices:
Use explicit waits to ensure elements are interactable
Use By.id locator for radio button
Use Page Object Model pattern for maintainability
Use JUnit assertions for validation
Automated Solution
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class RadioButtonTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testSelectMaleRadioButton() {
        driver.get("https://example.com/webform");

        // Wait until the radio button is clickable
        WebElement maleRadio = wait.until(
            ExpectedConditions.elementToBeClickable(By.id("gender_male"))
        );

        maleRadio.click();

        // Assert the radio button is selected
        assertTrue(maleRadio.isSelected(), "Male radio button should be selected after clicking");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with JUnit 5 to automate the radio button selection.

setUp(): Initializes ChromeDriver and WebDriverWait before each test.

testSelectMaleRadioButton(): Navigates to the web form page, waits explicitly for the radio button with id 'gender_male' to be clickable, clicks it, then asserts it is selected.

tearDown(): Closes the browser after the test to clean up.

Explicit waits ensure the element is ready before interaction, and using By.id is a best practice for locating elements reliably. The assertion confirms the radio button state matches expected behavior.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using XPath with absolute paths for locating radio buttons
Not verifying the radio button is selected after clicking
Bonus Challenge

Now add data-driven testing to select and verify three different radio buttons with ids 'gender_male', 'gender_female', and 'gender_other'.

Show Hint