0
0
Selenium Javatesting~10 mins

Checking state (isDisplayed, isEnabled, isSelected) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit
Selenium Java
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();
    }
}
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/checkboxpagePage with checkbox loaded in browser-PASS
3Finds checkbox element by id 'subscribeCheckbox'Checkbox element is located on the page-PASS
4Checks if checkbox is displayed using isDisplayed()Checkbox is visible on the pageassertTrue(checkbox.isDisplayed())PASS
5Checks if checkbox is enabled using isEnabled()Checkbox is enabled and can be clickedassertTrue(checkbox.isEnabled())PASS
6Checks if checkbox is selected using isSelected()Checkbox is not selected initiallyassertFalse(checkbox.isSelected())PASS
7Clicks the checkbox to select itCheckbox is now selected-PASS
8Verifies checkbox is selected after click using isSelected()Checkbox remains selected after clickassertTrue(checkbox.isSelected())PASS
9Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Checkbox element is not found or not visible/enabled as expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check first about the checkbox?
AIf the checkbox is visible on the page
BIf the checkbox is selected
CIf the checkbox is clickable
DIf the checkbox is checked by default
Key Result
Always verify the element's visibility, enabled state, and selection state before and after interaction to ensure UI elements behave as expected.