Bird
Raised Fist0
Selenium Javatesting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Selenium WebDriver method would you use to check if a button is visible on the web page?
easy
A. getText()
B. isEnabled()
C. isSelected()
D. isDisplayed()

Solution

  1. Step 1: Understand the purpose of isDisplayed()

    This method checks if an element is visible on the page, meaning it is rendered and not hidden.
  2. Step 2: Compare with other methods

    isEnabled() checks if the element can be interacted with, isSelected() checks selection state, and getText() returns text content, so they don't check visibility.
  3. Final Answer:

    isDisplayed() -> Option D
  4. Quick Check:

    Visibility check = isDisplayed() [OK]
Hint: Visibility means use isDisplayed() method [OK]
Common Mistakes:
  • Confusing isDisplayed() with isEnabled()
  • Using isSelected() for visibility
  • Thinking getText() checks visibility
2. Which of the following is the correct syntax to check if a checkbox element is selected in Selenium Java?
easy
A. checkbox.isSelected();
B. checkbox.isDisplayed();
C. checkbox.isEnabled();
D. checkbox.getSelected();

Solution

  1. Step 1: Recall the method to check selection state

    The correct method to check if a checkbox or radio button is selected is isSelected().
  2. Step 2: Verify syntax correctness

    checkbox.isSelected(); is valid Java syntax. getSelected() does not exist, and the others check different states.
  3. Final Answer:

    checkbox.isSelected(); -> Option A
  4. Quick Check:

    Check selection = isSelected() [OK]
Hint: Use isSelected() to check checkbox state [OK]
Common Mistakes:
  • Using getSelected() which is invalid
  • Confusing isDisplayed() with isSelected()
  • Using isEnabled() to check selection
3. Given the following code snippet, what will be the output if the button is visible but disabled?
WebElement button = driver.findElement(By.id("submitBtn"));
System.out.println(button.isDisplayed());
System.out.println(button.isEnabled());
medium
A. true true
B. true false
C. false true
D. false false

Solution

  1. Step 1: Understand isDisplayed() output

    The button is visible, so isDisplayed() returns true.
  2. Step 2: Understand isEnabled() output

    The button is disabled, so isEnabled() returns false.
  3. Final Answer:

    true false -> Option B
  4. Quick Check:

    Visible = true, Enabled = false [OK]
Hint: Visible true, enabled false if disabled button [OK]
Common Mistakes:
  • Assuming disabled means not displayed
  • Mixing up isDisplayed() and isEnabled() results
  • Expecting both to be true always
4. Identify the error in the following code snippet that checks if a radio button is selected:
WebElement radio = driver.findElement(By.name("gender"));
if(radio.isEnabled()) {
    System.out.println("Selected");
} else {
    System.out.println("Not Selected");
}
medium
A. Should use isSelected() instead of isEnabled()
B. Missing semicolon after findElement()
C. Incorrect locator By.name should be By.id
D. No error, code is correct

Solution

  1. Step 1: Understand the purpose of isEnabled() vs isSelected()

    isEnabled() checks if the element can be interacted with, not if it is selected.
  2. Step 2: Identify correct method to check selection

    To check if a radio button is selected, isSelected() must be used.
  3. Final Answer:

    Should use isSelected() instead of isEnabled() -> Option A
  4. Quick Check:

    Selection check = isSelected() [OK]
Hint: Use isSelected() to check radio button selection [OK]
Common Mistakes:
  • Using isEnabled() to check selection
  • Assuming enabled means selected
  • Ignoring method purpose differences
5. You want to write a test that clicks a checkbox only if it is visible, enabled, and not already selected. Which of the following code snippets correctly implements this logic?
hard
A. if(!checkbox.isDisplayed() && checkbox.isEnabled() && checkbox.isSelected()) { checkbox.click(); }
B. if(checkbox.isDisplayed() || checkbox.isEnabled() || checkbox.isSelected()) { checkbox.click(); }
C. if(checkbox.isDisplayed() && checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); }
D. if(checkbox.isDisplayed() && !checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); }

Solution

  1. Step 1: Understand the required conditions

    The checkbox must be visible (isDisplayed()), enabled (isEnabled()), and not selected (!isSelected()).
  2. Step 2: Analyze each option

    if(checkbox.isDisplayed() && checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); } uses AND (&&) to combine all correct conditions and negates isSelected() to ensure it is not selected before clicking.
  3. Final Answer:

    if(checkbox.isDisplayed() && checkbox.isEnabled() && !checkbox.isSelected()) { checkbox.click(); } -> Option C
  4. Quick Check:

    Use AND for all true and NOT for selection [OK]
Hint: Use && for all true and ! for not selected [OK]
Common Mistakes:
  • Using OR (||) instead of AND (&&)
  • Not negating isSelected() when needed
  • Clicking when checkbox is disabled or hidden
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard