Page Object Model and Page Factory
Feature | Page Object Model | Page Factory |
---|---|---|
Definition | Design pattern to create object repository for web UI elements | Extension of Page Object Model that uses @FindBy annotation |
Object Repository | Objects for each web page stored in separate class | Object repository created in the same class |
Locating elements | FindElement or FindElements used to locate elements | @FindBy annotation used to locate elements |
Example | public class LoginPage{<br>WebElement username = driver.findElement(By.id("username"));<br>WebElement password = driver.findElement(By.id("password"));} |
public class LoginPage{<br>@FindBy(id = "username") WebElement username;<br>@FindBy(id = "password") WebElement password;} |
Initialization | No need for initialization | Requires initialization using PageFactory.initElements() |
Usage | LoginPage login = new LoginPage();<br>login.username.sendKeys("name"); |
LoginPage login = PageFactory.initElements(driver, LoginPage.class);<br>login.username.sendKeys("name"); |