Page Object Model (POM)



This is a very important framework in selenium and many companies use this framework. Some of the advantages of Page Object Model are as follows:

  1. Every page in the product can have one class for selenium operation in Agile, as and when pages are created by developer, at the same time QA can start working on writing test cases for that page.

  2. Page Object Pattern says, operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.

  3. The Second benefit is that the object repository is independent of test cases, so we can use the same object repository for different purposes with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.

  4. The code becomes less lengthy and optimized because of the reusable page methods in the POM classes.

  5. Methods should be named more realistic names which can be easily mapped with the operation happening in the UI.
    For eg: If after clicking on the button we are redirected on the home page, then the method name will be like ‘navigateToHomePage()’.
    Simple and clear page classes with sensible method names.
    One can actually give the customized names to ones methods as mentioned above, so that one need not have to remember much.

    • Just looking into the method name gives all ideas about the capabilities of the method.

    • Makes tests more readable in comparison to the above commands of selenium, wherein one need to add all the commands in the test scripts. In page object model one needs to put method names. The methods which one has created according to one’s understanding of application then these method’s names become more readable and easy to understand.

  6. Stay DRY as page object model believes in the principle of Do not repeat yourself i.e. DRY.

  7. Better support of tests because everything is stored in one place.

  8. Easy creation of new tests. In fact, tests can be created by a person who is unaware of the features of automation tools.

  1. All locators should be kept in page class file.

  2. And this abstraction leads to some chaos within the Page Class file. So you need to implement something like a key word driven on top of Page Object Model so as to fully take the advantages.

  3. Requires quite good programming skills to design the automation suite (+ or – depending on the programming skills of the team).

Creating a Page Object Model in Java

  • Here, we’ll create an automation framework and implement Page Object Model using Selenium with Java.

  • Suppose we have to test a dummy application with only a login page and a home page, then after clicking on it user page will be displayed.

  • To start with we first need to create page objects for all available pages in our application such as ‘LoginPage.java’, ‘HomePage.java’ and ‘Userspage.java’.

  • Then we will create a test class that will create instances of these page objects and invoke their methods to create a test.

  • Let’s take the scenario wherein the ‘login page’ user enters valid credentials and on clicking ‘submit’ button, the user is redirected to the ‘home page’.

  • The ‘Page Object’ model works best when used in the right situation. It follows the lazy initialization design. So the WebDriver doesn’t load the elements until they come into the picture. This model is a by-product of the Factory Design Pattern. It is recommended to create a separate Factory Object to initialize the other objects.

  • This method leaves behind all other frameworks in terms of both maintenance and performance. Create a class called ‘LoginPage.java’ as shown below. Here we will be trying to show all features in the code. Remember, whenever we write code here, we must cover below points:

    1. Fetch all web elements of the web page.
      Elements like textboxes of username and password, labels of username and password and even we can locate all error messages that appears on the page. We also need to locate sign in button.

    2. All operations.
      Operations such as entering test into all textboxes, clicking a button and all other which we come across.

    3. Navigations from one page to other pages.
      Navigation can be done after clicking on any button by which we can go to ‘home page’. When we click on the back button on the home page, we will reach to login page.

    4. All verifications.
      Verifications needs to be done so that in the later stage, we can call those methods through testNG.

One by one we will be writing code for every page for our website.

Project structure looks as shown below:

page object model structure

LoginPage.java


HomePage.java


UsersPage.java

All above are pages, now we will write test cases for the same. I am using testNG to show report here.

You can proceed with writing more on this.
It is expected from learner of selenium to write at least 50 test cases on POM.