This test opens a page with two radio buttons, clicks each one, and checks if the selection updates correctly.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class RadioButtonTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com/radio-form");
WebElement option1 = driver.findElement(By.id("option1"));
WebElement option2 = driver.findElement(By.id("option2"));
// Click first radio button
option1.click();
// Assert option1 is selected
if (!option1.isSelected()) {
System.out.println("Test Failed: option1 should be selected.");
} else {
System.out.println("option1 is selected as expected.");
}
// Click second radio button
option2.click();
// Assert option2 is selected and option1 is not
if (option2.isSelected() && !option1.isSelected()) {
System.out.println("option2 is selected and option1 is not, as expected.");
} else {
System.out.println("Test Failed: Radio button selection did not update correctly.");
}
} finally {
driver.quit();
}
}
}