I am creating a Page Object framework while going through the concepts of it, I got to know that Page Factory(@FindBy) is used in conjunction with Page Objects. However, I am not able to understand why do I need to use @FindBy when I can use driver.findElement with my locators in Page Object class. For instance :
//Code with @FindBy
public class LoginPage{
public LoginPage(WebDriver driver)){
PageFactory.initElements(driver,this);
}
public WebElement q;
}
public class TestCase{
WebDriver driver=new FirefoxDriver();
LoginPage logPage=new LoginPage(driver);
public void enterUserName(){
logPage.q.sendKeys("username");
}
}
//Code with driver.findElement
public class LoginPage{
public WebElement q=driver.findElement(By.id('q'));
}
public class TestCase{
WebDriver driver=new FirefoxDriver();
LoginPage logPage=new LoginPage();
public void enterUsername(){
logPage.q.sendKeys("username");
}
}
What is the difference between both the codes over here as both the codes are essentially doing the same thing ?