Test Overview
This test checks if a hidden button can be found and clicked using Selenium in Java. It verifies that the button is not visible but still clickable by using JavaScript execution.
This test checks if a hidden button can be found and clicked using Selenium in Java. It verifies that the button is not visible but still clickable by using JavaScript execution.
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; 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.assertTrue; public class HiddenElementTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } @Test public void testClickHiddenButton() { driver.get("https://example.com/hidden-button"); // Locate the hidden button by id WebElement hiddenButton = driver.findElement(By.id("hidden-btn")); // Check that the button is not displayed assertTrue(!hiddenButton.isDisplayed(), "Button should be hidden"); // Click the hidden button using JavaScript JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", hiddenButton); // Verify that clicking the button triggered expected change WebElement message = driver.findElement(By.id("message")); assertTrue(message.isDisplayed(), "Message should be visible after click"); assertTrue(message.getText().contains("Clicked"), "Message text should confirm click"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/hidden-button | Page with hidden button loaded | - | PASS |
| 3 | Finds the hidden button element by id 'hidden-btn' | Hidden button element located but not visible | hiddenButton.isDisplayed() returns false | PASS |
| 4 | Asserts the button is hidden (not displayed) | Button is confirmed hidden | assertTrue(!hiddenButton.isDisplayed()) passes | PASS |
| 5 | Clicks the hidden button using JavaScript executor | Button click event triggered despite being hidden | - | PASS |
| 6 | Finds the message element by id 'message' | Message element is now visible on page | message.isDisplayed() returns true | PASS |
| 7 | Asserts the message is visible and contains 'Clicked' | Message confirms button click | assertTrue(message.getText().contains("Clicked")) passes | PASS |
| 8 | Test ends and browser closes | Browser closed cleanly | - | PASS |