0
0
Selenium Javatesting~3 mins

Why Checking state (isDisplayed, isEnabled, isSelected) in Selenium Java? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your tests could avoid hidden traps and only click buttons when they're truly ready?

The Scenario

Imagine you have a big web page with many buttons, checkboxes, and images. You want to make sure certain buttons are visible, enabled, or selected before clicking them. Doing this by looking at the screen and guessing is like trying to find a needle in a haystack.

The Problem

Manually checking if elements are visible or enabled is slow and tiring. You might miss something or make mistakes because the page changes quickly. It's easy to click a button that's hidden or disabled, causing errors and wasting time.

The Solution

Using methods like isDisplayed(), isEnabled(), and isSelected() in Selenium lets your test automatically check the state of elements. This means your test only acts when the element is ready, making your tests smarter and more reliable.

Before vs After
Before
if (element != null) {
  // guess if element is visible or enabled
  clickButton();
}
After
if (element.isDisplayed() && element.isEnabled()) {
  element.click();
}
What It Enables

This lets your tests interact only with elements that are ready, avoiding errors and making your automation trustworthy and fast.

Real Life Example

When testing a login page, you want to click the login button only if it is visible and enabled. Using isDisplayed() and isEnabled() ensures your test won't fail by clicking a hidden or disabled button.

Key Takeaways

Manual checks are slow and error-prone.

State-check methods automate and improve test reliability.

Tests become smarter by acting only on ready elements.