Verify interaction with a hidden element on a web page
Preconditions (2)
✅ Expected Result: The hidden button becomes visible after clicking 'Show', can be clicked, and triggers the expected action
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.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.time.Duration; public class HiddenElementTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testHiddenElementInteraction() { driver.get("https://example.com/test-hidden-element"); By showButtonLocator = By.id("show-button"); By hiddenButtonLocator = By.id("hidden-button"); By messageLocator = By.id("message"); // Assert hidden button is not visible initially assertTrue(driver.findElements(hiddenButtonLocator).size() > 0, "Hidden button should be present in DOM"); WebElement hiddenButton = driver.findElement(hiddenButtonLocator); assertFalse(hiddenButton.isDisplayed(), "Hidden button should not be visible initially"); // Click the 'Show' button WebElement showButton = driver.findElement(showButtonLocator); showButton.click(); // Wait until hidden button is visible wait.until(ExpectedConditions.visibilityOfElementLocated(hiddenButtonLocator)); // Click the now visible hidden button hiddenButton = driver.findElement(hiddenButtonLocator); // re-find element hiddenButton.click(); // Verify expected action: message appears WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(messageLocator)); assertEquals("Button clicked!", message.getText(), "Expected message after clicking hidden button"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and JUnit 5.
Setup: We create a ChromeDriver instance and a WebDriverWait with 10 seconds timeout.
Test steps:
Teardown: Close the browser after the test.
This approach avoids using Thread.sleep and uses explicit waits for reliability. It also re-finds the hidden button after it becomes visible to avoid stale element issues.
Now add data-driven testing with 3 different URLs where the hidden button behaves similarly
isDisplayed() method returns true if the element is visible on the page.isEnabled() checks if element is enabled, getText() gets text, and click() performs click action, none check visibility.WebElement element = driver.findElement(By.id("hiddenBtn"));
boolean visible = element.isDisplayed();
System.out.println(visible);isDisplayed() method returns false if the element exists but is hidden (not visible).findElement finds the element by id, no exception is thrown. The element is hidden, so isDisplayed() returns false.WebElement element = driver.findElement(By.id("hiddenBtn"));
element.click();element.click() causes ElementNotVisibleException because Selenium cannot interact with invisible elements.NoSuchElementException. It is not stale, so no StaleElementReferenceException.JavascriptExecutor: executeScript("arguments[0].scrollIntoView(true);", element);element.click() to interact safely with the visible element.Pass the test and this pattern moves unknown