What is Page Factory?

Page Factory gives an optimised way to implement Page Object Model. When we say it is optimised, it refers to the fact that the memory utilisation is very good and also the implementation is done in an object oriented manner.

Page Factory is used to initialise the elements of the Page Object or instantiate the Page Objects itself. Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to differentiate one object from the other.

The concept of separating the Page Object Repository and Test Methods is followed here also. Instead of having to use ‘FindElements’, we use annotations like: @FindBy to find WebElement, and initElements method to initialise web elements from the Page Factory class.

@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className & xpath as attributes.

Example:

public class MyClass
{
    public  MyClass(WebDriver driver)
    {
		PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

	@FindBy(css="")
	public RemoteWebElement myElement1;

    @FindBy(xpath="")
	public RemoteWebElement myElement2;
}