Page Object Model in Selenium is a design pattern to create an Object Repository for web UI elements.
However, Page Factory in Selenium is a built-in class for maintaining object repository. For that, we can import the package: Page Factory.
public class LogInPage
{
private WebElement usrnm;
private WebElement pwd;
public LogInPage() {
}
public void locateElements() {
usrnm = driver.findElement(By.id("userName"));
pwd = driver.findElement(By.id("password"));
}
public void doLogIn() {
usrnm.sendKeys("qwe");
pwd.sendKeys("123");
}
}
With Page Factory, initElement() statement can be used for easily looking up elements in page class.
Page Factory allows storing of page elements in cache memory using @CacheLookup annotation
So, which ever methods we have defined in a diff class, those can be imported by using the page factory library.
public class LogInPage
{
@FindBy(id="userName")
private WebElement usrnm;
@FindBy(id="password")
private WebElement pwd;
public LogInPage() {
PageFactory.initElements(driver, this); // initialize the members like driver.findElement()
}
public void doLogIn() {
usrnm.sendKeys("qwe");
pwd.sendKeys("123");
}
}