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:
- Which among the both options is a better choice?
- What are the trade-off's between both the following options?
Please explain