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 |
BasePage class in Selenium Java testing?this.driver = driver;.public void clickElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
System.out.println("Clicked successfully");
}public void enterText(By locator, String text) {
WebElement element = driver.findElement(locator);
element.sendKeys();
}element.sendKeys(); to element.sendKeys(text); to send the provided text.public boolean isElementVisible(By locator) that returns true if element is visible. How would you use this method in a LoginPage class to verify the login button is visible before clicking it?loginButtonLocator.clickElement(loginButtonLocator) only if the element is visible.Pass the test and this pattern moves unknown