0
0
Selenium Javatesting~15 mins

Checking state (isDisplayed, isEnabled, isSelected) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify visibility, enablement, and selection state of elements on the login page
Preconditions (2)
Step 1: Locate the username input field and verify it is displayed
Step 2: Locate the password input field and verify it is displayed
Step 3: Locate the login button and verify it is enabled
Step 4: Locate the 'Remember Me' checkbox and verify it is displayed and not selected
Step 5: Click the 'Remember Me' checkbox
Step 6: Verify the 'Remember Me' checkbox is now selected
✅ Expected Result: Username and password fields are visible, login button is enabled, 'Remember Me' checkbox is visible and toggles selection state correctly
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert username field is displayed
Assert password field is displayed
Assert login button is enabled
Assert 'Remember Me' checkbox is displayed
Assert 'Remember Me' checkbox is initially not selected
Assert 'Remember Me' checkbox is selected after clicking
Best Practices:
Use explicit waits to ensure elements are ready before interaction
Use meaningful locators such as By.id or By.name
Use assertions from a testing framework like TestNG or JUnit
Separate setup and teardown methods for browser management
Automated Solution
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;

public class LoginPageStateTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/login");
    }

    @Test
    public void testElementStates() {
        WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        Assert.assertTrue(usernameField.isDisplayed(), "Username field should be displayed");

        WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password")));
        Assert.assertTrue(passwordField.isDisplayed(), "Password field should be displayed");

        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn")));
        Assert.assertTrue(loginButton.isEnabled(), "Login button should be enabled");

        WebElement rememberMeCheckbox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("rememberMe")));
        Assert.assertTrue(rememberMeCheckbox.isDisplayed(), "Remember Me checkbox should be displayed");
        Assert.assertFalse(rememberMeCheckbox.isSelected(), "Remember Me checkbox should not be selected initially");

        rememberMeCheckbox.click();
        Assert.assertTrue(rememberMeCheckbox.isSelected(), "Remember Me checkbox should be selected after clicking");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with TestNG for assertions.

setUp() opens the browser and navigates to the login page.

testElementStates() waits explicitly for each element to be visible or clickable before checking its state:

  • Checks username and password fields are visible using isDisplayed().
  • Checks login button is enabled using isEnabled().
  • Checks 'Remember Me' checkbox is visible and initially not selected using isDisplayed() and isSelected().
  • Clicks the checkbox and verifies it is selected.

tearDown() closes the browser after the test.

Explicit waits ensure elements are ready to interact, avoiding flaky tests. Using meaningful locators like By.id improves reliability. Assertions provide clear pass/fail results.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not checking if elements are displayed before interacting
Not verifying the selection state after clicking a checkbox
Bonus Challenge

Now add data-driven testing with 3 different login page URLs to verify element states on each.

Show Hint