1

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

Bharat Mane
  • 6,775
  • 10
  • 40
  • 67
the_coder
  • 754
  • 1
  • 4
  • 13
  • PageFactory method offers more readability as in read them in English. – Yu Zhang Nov 07 '16 at 08:20
  • These pages give some information: http://stackoverflow.com/questions/9028757/what-is-the-use-of-annotation-findby and https://github.com/SeleniumHQ/selenium/wiki/PageFactory , but not enough to make a good answer out of it. – Niels van Reijmersdal Nov 07 '16 at 09:35
  • One thing that i understood from above links is that, PageFactory concept is less verbose compared to storing elements in By locators. But are there any important points that i am missing? – the_coder Nov 07 '16 at 11:03

0 Answers0