PageFactory in selenium

PageFactory is an extended support to POM (PageObjectModel), used to locate webelement in adifferent manner than mentioned here.

Before we learned to declare and locate an WebElement as below :

Using PageFactory

@FindBy

or this can even read as,

The above 2 lines are not sufficient to locate, we need to instantiate the class object through PageFactory, so this is as follows

“this”, as we want to instantiate the webelement is the same class.

If the pagefactory elements are in different class or java file, let’s see how we can access

LoginPage.java – page object class that holds all the weblements and methods related to login page

LoginPageTest.java – this is the test case which actually instantiate LoginPage class object and weblements

some more PageFactory insights


Supported locators

@FindBy(“locatorName”, “value”)
locator name could be any one of the following:
ID, CLASS_NAME, NAME, CSS, PARTIAL_LINK_TEXT, TAG_NAME, XPATH

@CacheLookup

Every time we use (use only, not declaring multiple times) the WebElement, it searches if element present or not,
but if we are sure element is present on current page, then use @CacheLookup to avoid checking the presence and save some time… code as follows

@FindBys

This will return list of webElements matching all the conditions [AND operator]

@FindAll

This will return list of webElements matching any of the [at least one] condition [OR operator]

Reference : https://github.com/SeleniumHQ/selenium/wiki/PageFactory

Content posted is based on learning or working experience, Share the post with others if find useful, Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

1 Response

  1. June 7, 2016

    […] PageFactory post for more […]

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: