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 |
@FindBy annotation in Selenium Java?@FindBy@FindBy annotation is used to specify how to find a web element on a page, such as by id, name, xpath, etc.@FindBy annotation?@FindBy annotation requires the attribute name and value as a string. For ID, it is id = "value".id = "submitBtn". @FindBy(name = submitBtn) misses quotes. @FindBy(xpath = submitBtn) uses xpath but without quotes. @FindBy(css = #submitBtn) uses CSS selector but with incorrect syntax (should be css = "#submitBtn").button.isDisplayed() is called?public class LoginPage {
@FindBy(name = "loginBtn")
WebElement button;
public LoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
// In test method:
LoginPage page = new LoginPage(driver);
boolean visible = page.button.isDisplayed();
System.out.println(visible);PageFactory.initElements(driver, this), which initializes all @FindBy annotated elements, including button.isDisplayed()isDisplayed() returns true; otherwise false. No NullPointerException occurs because initialization is done.@FindBy:public class HomePage {
@FindBy(xpath = "//button[@id='start']")
WebElement startButton;
public HomePage(WebDriver driver) {
// Missing initialization
}
}PageFactory.initElements(driver, this), so startButton remains null.@FindBy. Which declaration is correct to store all matching elements in a list?public class ProductsPage {
// Which line is correct?
}List<WebElement>. Using @FindBy with className locator is valid.List<WebElement> with correct locator. Options A, B, and D use single WebElement, which can only hold one element.Pass the test and this pattern moves unknown