Test Overview
This test uses the @FindBy annotation to locate a login button on a web page and verifies that the button is clickable and has the correct label.
This test uses the @FindBy annotation to locate a login button on a web page and verifies that the button is clickable and has the correct label.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; public class LoginPageTest { WebDriver driver; public static class LoginPage { @FindBy(id = "loginBtn") WebElement loginButton; public LoginPage(WebDriver driver) { PageFactory.initElements(driver, this); } public void clickLogin() { loginButton.click(); } public String getLoginButtonText() { return loginButton.getText(); } } LoginPage loginPage; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/login"); loginPage = new LoginPage(driver); } @Test public void testLoginButton() { assertTrue(loginPage.loginButton.isDisplayed(), "Login button should be visible"); assertEquals("Log In", loginPage.getLoginButtonText(), "Login button text should be 'Log In'"); loginPage.clickLogin(); // Additional assertions can be added here after click } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/login | Login page is loaded in the browser | - | PASS |
| 3 | PageFactory initializes elements annotated with @FindBy | loginButton WebElement is located by id 'loginBtn' | loginButton element is found and ready | PASS |
| 4 | Checks if loginButton is displayed | Login button is visible on the page | assertTrue(loginButton.isDisplayed()) verifies visibility | PASS |
| 5 | Gets text of loginButton and verifies it equals 'Log In' | Login button text is 'Log In' | assertEquals('Log In', loginButton.getText()) verifies label | PASS |
| 6 | Clicks the loginButton | Login button is clicked, triggering login action | - | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |