Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 905
Haendler Avatar asked Sep 20 '17 18:09

Haendler


People also ask

What is the use of Page factory in Java?

- Quora What is Page Factory? Page Factory, an extension to Page Objects, is used mainly for the initialization of web elements specified in page classes (or page objects). Web elements used for Page Objects must be initialized until they can be used further and Page Factory simplifies initialization with the initElements method.

What is the advantage of using Page factory pattern?

One of the key advantages of using Page Factory pattern is AjaxElementLocatorFactory Class. It is working on lazy loading concept, i.e. a timeout for a WebElement will be assigned to the Object page class with the help of AjaxElementLocatorFactory .

What is Page factory in selenium?

Page Factory and Page Object Model (POM) are design patterns that make it easy for QA engineers to maintain and re-use code in a more effective manner. Page Factory in Selenium provides more firepower to POM due to the support of important annotations and methods that simplify locating and initializing the web element variables.

Is pagefactory deprecated in net?

I was using for a long time PageFactory (in Java). Now writing tests in .NET I found out it's deprecated. Right now I just initialize all page objects when application starts using [BeforeScenario] SpecFlow annotation.


1 Answers

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.

like image 73
AndrewP Avatar answered Oct 08 '22 20:10

AndrewP