Page Object Pattern/Page Object Model in automation test.

Tram Ho

What is the Page Object Pattern?

Page Object Pattern is also known as POM (Page object model). Here we call it Page Object Pattern because POM coincides with another keyword in maven which is also POM (Project Object Model) so in this article I will mention it under POP keyword.

Page Object Pattern (POP) is a design pattern that helps in automation testing. While Selenium POP plays the role of creating an archive of all web page elements , POP is useful in reducing code duplication and improving maintainability for test cases. In POP, think of an application’s web page as a class file . Each class file will contain only the elements of the corresponding web page. Using these elements, the Tester can perform operations on the tested website.

image.png

In addition, we will have other frameworks that we will learn in the following article: Singleton Pattern, Strategy Pattern, Factory Pattern.

Advantages of Page Object Pattern.

Minimize code duplication:

  • With Selenium API we will code as follows: * WebElement element = driver.findElement(By.xxx("")) * element.sendKeys()/click()/getText() image.png Using the Selenium API directly causes code duplication and is very difficult to maintain because when there is a change in an element, it will have to find each line containing that element to fix it.
  • With the Page Object Pattern We will optimize the above disadvantage by creating Functions that can be used for many different Test Cases. Untitled Diagram.drawio.png Just pass in the parameters to create different test cases. image.png

Easy to maintain, extend the code later:

Testcase is short, easy to read, methods correspond to manual actions on each page.

Disadvantages of Page Object Pattern.

The human factor is very important:

  • Need coding, technical, backgroud skills for automation testing.
  • Not suitable for people/teams who don’t know code, don’t know technical.
  • Know how to write common functions
  • Know how to share pages
  • Know locator definition and locator optimization
  • Know what constructor is
  • Know global/local variables
  • Know when to initialize the page / initialize at what position / this page calls to another page
  • Know how to inherit class/scope
  • Know how to arrange test cases in the right flow

It takes a long time to build the framework

  • For experienced people, it takes about 3 days to clone + write 5-10 test cases
  • Build from scratch about 1 week – 10 days – 1 month depending on level

The number of actions of each page is too much as test cases and functionality increase.

  • Declare and implement a lot of functions for fields
  • Declare lots of locators for fields screenshot-tiemchungcovid19.gov.vn-2022.09.03-10_55_33.png

Solution

Use dynamic to handle (Page/ Action/ Component)

  • Use [Framework] Page Element Component to redesign page object, page UI – Write function in component form, not write in action image.png

Note

  • The testcase layer should not have API code (TestNG, Webdriver API) or code like if else/ do/ while… should only be in the page object layer
  • Page object layer should not have Assert and Verify hames => only used in testcase layer
    • If the action layer returns, it should return values ​​for the testcase layer to verify. 2.png
  • Each page object writes only the action of the page itself.
  • How to write action/UI to that do not write all action/UI of a page. image.png

     

  • From 1 page A, when switching to page B, it is necessary to initialize that page B and then proceed with the steps of page B otherwise it will lead to a NullPointerException error.
  • To ensure the right flow like manual test, when you go to any page, you should return that page.
  • Understand the life cycle, how to interact, and transfer drivers between pages
    • **Driver => AbtractTest => TestCase => Page Object => AbtractPage **

In the next article, I will continue to talk about the Singleton Pattern. Thanks!

Share the news now

Source : Viblo