1

This question already has an answer here:

I know that elements in page objects can be initialized and used in both the following ways

Option 1:

public class LoginPage 
{
   WebDriver driver;
   By usernameLocator = By.id("username");
   By passwordLocator = By.id("password");
   By loginButton = By.className("radius");

 public LoginPage(WebDriver driver)
 {
    this.driver=driver;
 }
 public void with(String username,String password)
 {
    driver.findElement(usernameLocator).sendKeys(username);
    driver.findElement(passwordLocator).sendKeys(password);
    driver.findElement(loginButton).click();
 }
}

and also using PageFactory concept

Option 2:

public class LoginPage 
{

 WebDriver driver;

 @FindBy(how=How.ID, using="username")
 public WebElement usernameLocator;

 @FindBy(how=How.ID, using="password")
 public WebElement passwordLocator;

 @FindBy(how=How.CLASS_NAME, using="radius")
 public WebElement loginButton;

 public LoginPage(WebDriver driver)
 { 
    this.driver=driver;     
    //Initialize elements
    PageFactory.initElements(driver, this);
 }

 public void with(String username, String password)
 {
    usernameLocator.sendKeys(username);
    passwordLocator.sendKeys(password);
    loginButton.click();
 }
}

In the above two options:

  1. Which among the both options is a better choice?
  2. What are the trade-off's between both the following options?

Please explain

marked as duplicate by Bharat Mane, Yu Zhang, IAmMilinPatel, NarendraC, Kate Paulk Dec 22 '16 at 12:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.