Automation Using Selenium Webdriver
Showing posts with label @FindBy annotations Using in Java. Show all posts
Showing posts with label @FindBy annotations Using in Java. Show all posts

Tuesday 18 October 2016

@FindBy annotations Using in Java

@FndBy Annotations Using

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
 
    @FindBy(id="loginInput") //method used to find WebElement, in that case Id
    WebElement loginTextbox;
 
    @FindBy(id="passwordInput")
    WebElement passwordTextBox;
 
    @FindBy(xpath="//form[@id='loginForm']/button(contains(., 'Login')") //method = xpath
    WebElement loginButton;
 
    public void login(String username, String password){
        // login method prepared according to Page Object Pattern
        loginTextbox.sendKeys(username);
        passwordTextBox.sendKeys(password);
        loginButton.click();
        // because WebElements were declared with @FindBy, we can use them without
        // driver.find() method
    }
}

And Tests class:

class Tests{
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        LoginPage loginPage = new LoginPage();
        PageFactory.initElements(driver, loginPage); //PageFactory is used to find elements with @FindBy specified
        loginPage.login("user", "pass");
    }
}