Test Overview
This test verifies that the Page class correctly locates and interacts with the login button on a web page. It checks that clicking the button triggers the expected behavior.
This test verifies that the Page class correctly locates and interacts with the login button on a web page. It checks that clicking the button triggers the expected behavior.
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.*; class LoginPage { private WebDriver driver; private By loginButtonLocator = By.id("login-btn"); public LoginPage(WebDriver driver) { this.driver = driver; } public WebElement getLoginButton() { return driver.findElement(loginButtonLocator); } public void clickLogin() { getLoginButton().click(); } } public class LoginPageTest { private WebDriver driver; private LoginPage loginPage; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/login"); loginPage = new LoginPage(driver); } @Test public void testClickLoginButton() { loginPage.clickLogin(); String currentUrl = driver.getCurrentUrl(); assertTrue(currentUrl.contains("dashboard"), "URL should contain 'dashboard' after login click"); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | — | PASS |
| 2 | Browser opens ChromeDriver | Chrome browser window opens | — | PASS |
| 3 | Navigates to https://example.com/login | Login page is loaded in browser | Page URL is https://example.com/login | PASS |
| 4 | LoginPage object is created with driver | Page class ready to interact with elements | — | PASS |
| 5 | Finds login button by id 'login-btn' | Login button element is located on page | Element is present and visible | PASS |
| 6 | Clicks login button | Browser processes click event | — | PASS |
| 7 | Checks current URL contains 'dashboard' | Browser URL updated after click | assertTrue(currentUrl.contains("dashboard")) | PASS |
| 8 | Test ends and browser closes | Browser window closed, resources freed | — | PASS |
loginPage.getTitle() return if the page title is "Welcome Page"?public class LoginPage {
private WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public String getTitle() {
return driver.getTitle();
}
}public void clickLogin() {
WebElement loginBtn = driver.findElement(By.id("login"));
loginBtn.click();
}Pass the test and this pattern moves unknown