Test Overview
This test checks if a checkbox is visible, enabled, and selected on a web page. It verifies the checkbox's display, enabled state, and selection state to ensure it behaves as expected.
This test checks if a checkbox is visible, enabled, and selected on a web page. It verifies the checkbox's display, enabled state, and selection state to ensure it behaves as expected.
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.*; public class CheckboxStateTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/checkboxpage"); } @Test public void testCheckboxStates() { WebElement checkbox = driver.findElement(By.id("subscribeCheckbox")); // Check if checkbox is displayed assertTrue(checkbox.isDisplayed(), "Checkbox should be visible"); // Check if checkbox is enabled assertTrue(checkbox.isEnabled(), "Checkbox should be enabled"); // Check if checkbox is not selected initially assertFalse(checkbox.isSelected(), "Checkbox should not be selected initially"); // Click the checkbox to select it checkbox.click(); // Verify checkbox is selected after click assertTrue(checkbox.isSelected(), "Checkbox should be selected after clicking"); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | — | PASS |
| 2 | Navigates to https://example.com/checkboxpage | Page with checkbox loaded in browser | — | PASS |
| 3 | Finds checkbox element by id 'subscribeCheckbox' | Checkbox element is located on the page | — | PASS |
| 4 | Checks if checkbox is displayed using isDisplayed() | Checkbox is visible on the page | assertTrue(checkbox.isDisplayed()) | PASS |
| 5 | Checks if checkbox is enabled using isEnabled() | Checkbox is enabled and can be clicked | assertTrue(checkbox.isEnabled()) | PASS |
| 6 | Checks if checkbox is selected using isSelected() | Checkbox is not selected initially | assertFalse(checkbox.isSelected()) | PASS |
| 7 | Clicks the checkbox to select it | Checkbox is now selected | — | PASS |
| 8 | Verifies checkbox is selected after click using isSelected() | Checkbox remains selected after click | assertTrue(checkbox.isSelected()) | PASS |
| 9 | Test ends and browser closes | Browser window is closed | — | PASS |
isDisplayed()isEnabled() checks if the element can be interacted with, isSelected() checks selection state, and getText() returns text content, so they don't check visibility.isSelected().checkbox.isSelected(); is valid Java syntax. getSelected() does not exist, and the others check different states.WebElement button = driver.findElement(By.id("submitBtn"));
System.out.println(button.isDisplayed());
System.out.println(button.isEnabled());isDisplayed() returns true.isEnabled() returns false.WebElement radio = driver.findElement(By.name("gender"));
if(radio.isEnabled()) {
System.out.println("Selected");
} else {
System.out.println("Not Selected");
}isEnabled() checks if the element can be interacted with, not if it is selected.isSelected() must be used.isDisplayed()), enabled (isEnabled()), and not selected (!isSelected()).isSelected() to ensure it is not selected before clicking.Pass the test and this pattern moves unknown