Skip to main content

Development

Page Object and Page Factory Pattern Based UI Automated Testing

1.      Introduction

The Page Object Model (POM) is a very important pattern in selenium webdriver. It can also be applied to most of UI automated testing, even with other tools like UFT, Appium and so on. Page Factory is an inbuilt and optimized concept of POM. Page Factory can be used in many frameworks such as Data Driven, Behavior Driven, Modular or Keyword Driven. By integrating POM and Page Factory with the Test Case Model, you receive more focus on how the code is being structured to get the most benefits.              

2.      Advantages

Page Object can be considered as offering the “services” which a page offers rather than exposing the details and UI structure of the page. It can separate the page operations from complicated business logic in the test case. When the change happens in page, you only need to update the page object rather than tens or hundreds of test cases.

Page Factory can initiate elements to create an object repository with annotations @FindBy. It is a lazy way of loading to identify WebElements only with AjaxElementLocatorFactory, when they are used in any operation. In other words,  it will find it again whenever we use a Webelement. So you won’t see StaleElementExceptions with PageFactory, but you may see them by using driver.findElement in the example below.

Two elements are defined by PageFactory and FindElement respectively in the product page.

Then use PageFactory to initiate the product element.

Then use the above two elements in methods. The page is refreshed after click the sorting link.

3.       Architecture

Besides PageObject, Pagemanager is an extended Page Factory Concept, because it contains more operations of the driver and comprise methods of every page. Add the TestCaseBase and Reporter component and we will see Page Factory automated testing architecture in detail. If we want to apply the BDD concept, more classes for step and scenario can be added.

Page Object and Page Factory Pattern Based UI Automated Testing

3.1 PageObject

As you see above, PageFactory initiates every element defined in PageObject at first instead of calling the findElement method for every element. It helps the tester to focus on the operation instead of locating elements. For testing across platforms, different annotations can support when defining elements in Pages, i.e., @AndroidFindBy, @FindBy and @iOSFindBy. It is easier to integrate multiple platforms to a unified testing harness.  For each page in the application, we have a corresponding Page class and encapsulate operations to methods.

“Page object” may be misleading because you think it is just a physical page. Our practices tell that it should be treated as a portlet which may be used across pages, like a navigation bar.

3.2 PageManager

In PageManager Class, methods of elements are redefined, like Click, ClickByAction and so on. We can customize the operations with smart waiting, logging and reporting functionalities. It’s also very easy to upgrade a driver or change a driver.

3.3 TestCase

The TestCaseBase class aggregates driver, pageObject, pageManager, assertion and report together. Every test case needs to define pages and call methods to comprise steps, then add an assertion to verify the result.

3.4 Report

CustomAssertion and ComplexReportFactory are designed to integrate Extentreport, so that we can have a dynamic and visual report and dashboard. It can be replaced with other reporting tools.

4.      Conclusion

The Page Object Model can organize elements across different platforms, and Page Factory can locate elements dynamically. It is popularized by selenium webdriver, but it can be regarded as a common model to design a UI testing model.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Cathy Zhang

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram