Test Overview
This test uses the base page class pattern to open a web page, find a button, click it, and verify the resulting message is displayed correctly.
This test uses the base page class pattern to open a web page, find a button, click it, and verify the resulting message is displayed correctly.
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; // Base page class with common methods class BasePage { protected WebDriver driver; public BasePage(WebDriver driver) { this.driver = driver; } public void click(By locator) { driver.findElement(locator).click(); } public String getText(By locator) { return driver.findElement(locator).getText(); } } // Home page class extending base page class HomePage extends BasePage { private By buttonLocator = By.id("show-message-btn"); private By messageLocator = By.id("message"); public HomePage(WebDriver driver) { super(driver); } public void clickShowMessage() { click(buttonLocator); } public String getMessageText() { return getText(messageLocator); } } public class BasePagePatternTest { private WebDriver driver; private HomePage homePage; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/testpage"); homePage = new HomePage(driver); } @Test public void testShowMessageButton() { homePage.clickShowMessage(); String message = homePage.getMessageText(); assertEquals("Hello, World!", message); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | - | PASS |
| 2 | Browser navigates to https://example.com/testpage | Page loads with a button having id 'show-message-btn' and hidden message with id 'message' | - | PASS |
| 3 | HomePage object created with driver | Page elements accessible via HomePage methods | - | PASS |
| 4 | Clicks the button with id 'show-message-btn' using BasePage click method | Button is clicked, message becomes visible | - | PASS |
| 5 | Gets text from element with id 'message' using BasePage getText method | Message text is 'Hello, World!' | Verify message text equals 'Hello, World!' | PASS |
| 6 | Assertion checks if actual message text matches expected | Test verifies message correctness | assertEquals("Hello, World!", message) passes | PASS |
| 7 | Browser closes and test ends | Browser window closed | - | PASS |