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 |
click() and sendKeys() simulate real user behavior?sendKeys().element.sendKeys("Hello World") sends keystrokes to the element correctly.click() method?WebElement button = driver.findElement(By.id("submitBtn"));
button.click();
String message = driver.findElement(By.id("msg")).getText();
System.out.println(message);click() doesclick() method simulates a user clicking the button, triggering any attached actions.WebElement btn = driver.findElement(By.id("loginBtn"));
driver.click(btn);click() method is not defined on the driver object to accept a WebElement parameter.click() directly on the WebElement, like btn.click();.click() and sendKeys() instead of directly modifying the DOM with JavaScript for testing user actions?click() and sendKeys() simulate real user actions, triggering browser events and UI changes.Pass the test and this pattern moves unknown