0
0
Selenium Javatesting~10 mins

@FindBy annotations in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium
Selenium Java
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();
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/loginLogin page is loaded in the browser-PASS
3PageFactory initializes elements annotated with @FindByloginButton WebElement is located by id 'loginBtn'loginButton element is found and readyPASS
4Checks if loginButton is displayedLogin button is visible on the pageassertTrue(loginButton.isDisplayed()) verifies visibilityPASS
5Gets text of loginButton and verifies it equals 'Log In'Login button text is 'Log In'assertEquals('Log In', loginButton.getText()) verifies labelPASS
6Clicks the loginButtonLogin button is clicked, triggering login action-PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The element with id 'loginBtn' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @FindBy annotation do in this test?
AStarts the browser
BLocates the web element by a specified selector before test actions
CClicks the button automatically
DVerifies the button text
Key Result
Using @FindBy annotations with PageFactory helps organize element locators clearly and initializes them before test actions, improving code readability and maintainability.