Test Overview
This test opens a web page, finds a button, clicks it, and verifies the expected message appears. It shows how interaction methods simulate real user actions to test the app realistically.
This test opens a web page, finds a button, clicks it, and verifies the expected message appears. It shows how interaction methods simulate real user actions to test the app realistically.
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.assertEquals; public class UserInteractionTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testButtonClickShowsMessage() { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.id("showMessageBtn")); button.click(); WebElement message = driver.findElement(By.id("message")); String text = message.getText(); assertEquals("Hello, user!", text); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/testpage | Page loads with a button labeled 'Show Message' | - | PASS |
| 3 | Finds button element by id 'showMessageBtn' | Button element is located on the page | - | PASS |
| 4 | Clicks the button to simulate user click | Button is clicked, triggering message display | - | PASS |
| 5 | Finds message element by id 'message' | Message element is present after click | - | PASS |
| 6 | Gets text from message element | Text 'Hello, user!' is retrieved | Verify text equals 'Hello, user!' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |