'Why use Page Factory?

I am a new test engineer and have been reading about Page Object Model and implementing them and keep coming across Page Factory. I understand that Page Factory is a POM that provides additional features such as instantiating all elements when calling Page Factory and more readable code for tests (though I'm not completely sold on the readability). To be clear, I'm sold on POM. The reusability of the code and the relative ease of maintenance are great and I'm building in that direction.

The two questions I have come down to:

  • Why do I want to instantiate all the elements instead of doing it on the fly?
  • What are the advantages of Page Factory that I'm missing?


Solution 1:[1]

Here's Simon Stewart, Selenium project lead and creator of the Page Factory, at the 2017 SeleniumConf in Austin. During his keynote address he says not to use Page Factory. This section of the talk starts here:

https://youtu.be/gyfUpOysIF8?t=1517

Actual statement is at 27:25.

Solution 2:[2]

A few answers have said that the PageFactory "loads" all the WebElements when it is instantiated - this is not actually correct.

The elements don't load until you access them. It's done through the base classes and a RealProxy. It does use the standard FindElement(s)By methods under the hood, so there is no real performance benefit to having WebElements vs storing the By's and loading them when you need them.

One reason I choose to not use the PageFactory model, is I may have a search for an element that I don't want to exist, and by using the auto-wired approach, it searches to see if it exists before I can say "doesn't exist" in the test.

Another issue is there are subtle differences between how the PageFactory instantiates the WebElement and how Driver.FindBy instantiates them. One that bugs me is that PageFactory's version doesn't implement IWrapsDriver, meaning you can't get the driver used to find the element from the element. This may not seem like much, but it means when you want to write extensions to WebElement methods that in turn need a driver, you have to work out a (much more complicated) way of getting the driver, especially since I believe the PageObjectModel should not have a direct reference to the driver...

But that said, for a lot of cases, the out of the box PageFactory approach is very good. I think that the key to using- or not using- the PageFactory is just to have a consistent approach to how your test code and page object models work and interract - as that is key to maintainability.

Solution 3:[3]

Why do I want to instantiate all the elements instead of doing it on the fly?

If I remember correctly, PageFactory scans for any WebElement properties/fields and their attributes and wraps them with a proxy. At that point you're not touching Selenium server yet (you can check this in server console output). Once you try to access the property the WebElement gets instantiated. So if you only accessed one PO property/field only for that one WebElement is created.

What are the advantages of Page Factory that I'm missing?

The use of attributes make the code much more readable and also easy to generate. It is common to create a tool that generates PageObjects for you.

PageFactory was created to support PageObject pattern, nothing more. You don't have to necessarily use it in order to go PO way.

Finally, if you're curious about how it works in detail, I'd suggest you check the source code. It's what I did when I was just starting with Selenium.

Solution 4:[4]

Why do I want to instantiate all the elements instead of doing it on the fly?

In general, when we initiate any page class through PageFactory, it enables us to load all the desired (defined) WebElements at the same time with which we intend to interact in the coarse of Test Execution.

What are the advantages of Page Factory that I'm missing?

The advantages of using Page Factory is numerous. Some of them are as follows:

  1. Page Factory makes your framework more structured, robust and maintainable.
  2. Changes in the DOM Tree of any individual/multiple page can be accommodated with quite ease.
  3. Through Page Factory, the different calls between @Test class, BrowserFactory, Page Objects and Assertions becomes more cleaner and efficient.
  4. The additional attributes of Page Factory like FindBy(), FindBys, and CacheLookup speeds up our test execution by a huge extent.

Solution 5:[5]

  • Why do I want to instantiate all the elements instead of doing it on the fly?

    The PageFactory can be used to initialize elements of a Page class without having to use FindElement or FindElements. When you are using the webelement(s) more then once your code gets better readable.

  • What are the advantages of Page Factory that I'm missing?

    You also can use some attributes with the Page Factory. The most obvious is the FindsBy attribute but you can also use the CacheLookup attribute to cache the element once it is looked up once.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AndrewP
Solution 3 frennky
Solution 4
Solution 5