Test Overview
This test opens a web page with radio buttons, selects a specific radio button, and verifies that it is selected.
This test opens a web page with radio buttons, selects a specific radio button, and verifies that it is selected.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class RadioButtonTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/radio-buttons"); } @Test public void testSelectRadioButton() { WebElement radioButton = driver.findElement(By.id("option2")); radioButton.click(); assertTrue(radioButton.isSelected(), "Radio button option2 should be selected"); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open, ready to navigate | — | PASS |
| 2 | Navigates to https://example.com/radio-buttons | Page with radio buttons is loaded and visible | Page title or URL confirms correct page loaded | PASS |
| 3 | Finds radio button element with id 'option2' | Radio button with id 'option2' is located in the DOM | Element is present and interactable | PASS |
| 4 | Clicks on the radio button 'option2' | Radio button 'option2' is selected visually | — | PASS |
| 5 | Checks if radio button 'option2' is selected | Radio button 'option2' state is selected | assertTrue(radioButton.isSelected()) verifies selection | PASS |
| 6 | Test ends and browser closes | Browser window is closed | — | PASS |
isSelected() method do when used on a radio button element in Selenium?isSelected()click() selects the radio button, but isSelected() only checks selection status without changing it.isSelected() = Check selection status [OK]click() method on the WebElement.isSelected() only checks selection, getText() returns label text, and sendKeys() is not used for radio buttons.click() to select radio button [OK]WebElement rb1 = driver.findElement(By.id("radio1"));
WebElement rb2 = driver.findElement(By.id("radio2"));
rb1.click();
boolean selected1 = rb1.isSelected();
boolean selected2 = rb2.isSelected();
What will be the values of selected1 and selected2 after execution?rb1 selects it and deselects other radio buttons in the same group, so rb1.isSelected() returns true and rb2.isSelected() returns false.selected1 is true and selected2 is false after clicking rb1.WebElement radio = driver.findElement(By.name("gender"));
radio.click();By.name("gender") returns the first matching element, but if multiple radio buttons share the same name, this may select the wrong one.By.name returns multiple elements; need to specify which one -> Option CPass the test and this pattern moves unknown