7

I'm starting my career as a QA Automation Engineer coming from a Jr dev background. I noticed that there are two main design patterns for Selenium, POM and PageFactory.

  • Which is the preferred design pattern to use in best practice?
  • Also, there are several ways of finding an HTML element. Which one tends to be the best practice rule of finding an element? Is it ID?

As for Page Factory, my understanding is that for each webpage, you create a class representation of said webpage, and within the class, you create methods that act upon the webelements. You then will have different test classes that do the actual testing. Please correct or confirm my understanding.

7
  • have you tried spending at least 2 minutes with google before asking this question?
    – George
    Commented Dec 29, 2017 at 11:34
  • 3
    No, I decided to type two paragraphs on the matter for the hell of it.
    – chris
    Commented Dec 29, 2017 at 11:39
  • Here is an answer on first part: stackoverflow.com/questions/35866113/…
    – George
    Commented Dec 29, 2017 at 11:43
  • Thanks, it seems like they are identical/doing the same thing. The only difference looks like that in Page Factory (like the poster mentioned) that it is initializing the elements to be used. So, I'm guessing usually Page Factory is mostly used, or depending on the situation?
    – chris
    Commented Dec 29, 2017 at 11:49
  • 1
    Don't ask two questions in one. Best ways to find elements is answered here already: sqa.stackexchange.com/questions/27978/… Commented Dec 29, 2017 at 12:08

1 Answer 1

7

I think the Page Factory is an extension of the Page Object Model. This Selenium documentation page describes with examples what the Factory adds.

If you use the PageFactory, you can assume that the fields are initialised. If you don't use the PageFactory, then NullPointerExceptions will be thrown if you make the assumption that the fields are already initialised.

Guess that is the biggest win.

Personally I couldn't really care for the PageFactory, just adds magic and makes stuff more complex for new developers/testers.

Within your web app's UI there are areas that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.

Read more here...

I do love the Page Objects because it lowers maintenance and makes the tests more readable.

10
  • Yeah, that's what I was assuming. From my understanding, the main significant difference is that the elements are already initialized if a page factory design pattern is being used. Hence the PageFactory.initElements(driver, this); method call. I'm just curious as to learning what is most used in the automation field as a design pattern, since I'll be starting off my career in it pretty soon. :) Do you yourself typically use Page Objects or Page Factory typically?
    – chris
    Commented Dec 29, 2017 at 12:02
  • Based on that fact that the factory extends the object-model. I would guess the object-model must be more popular. You can always later switch to the factory pattern. Not using the object-model seems unacceptable for my projects, not using the factory... Commented Dec 29, 2017 at 12:06
  • Makes sense. It would also seem logically, that if you have a large webpage and have a bunch of elements and are using Page Factory, all those elements would be initialized within the constructor. Thus, slowing down the automation software requiring linear complexity time to the amount of elements (Big O N).
    – chris
    Commented Dec 29, 2017 at 12:10
  • 1
    it will be initialized in a sense that variable will have assigned value, but not in a sense that variable will hold actual webelement. That happens only when interacting with it.
    – George
    Commented Dec 29, 2017 at 12:20
  • 2
    @chris It's called lazy loading or lazy initialization. It's lazy because unless you interact with it it won't be initialized, i.e., it won't search for matching element in HTML.
    – dzieciou
    Commented Dec 29, 2017 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.