Page Object Model with JAVA


Hi all, in this tutorial I will explain you the most common test automation design pattern Page Object Model. Before, Ege Aksöz also wrote an article on POM with C#.  You can also see the definition of POM and the main advantages. Thus, I do not go into fundamentals again. I just want to show you a real life basic POM example with JAVA.

Our POM framework generally consists of 3 basic abstraction layer. These are tests, pages, and util. Complicated frameworks comprise more abstraction layers. We start from the basics so we will have three abstraction layers. In this article, I will not write any class in Util layer but we will improve our POM framework in later articles and I will add new utility classes in this layer.

Tests: It comprises of our test classes.

Pages: It comprises of page classes.

Util: It comprises of utility and helper classes.

I will select the one of the most popular Turkish e-commerce site n11.com as a test website and we will implement basic failed login scenarios with POM & JAVA & TestNG. Let’s Start!

At the end of the project, our project structure will be like this.

First, I will start with BaseTest class. It contains all common functionalities and variables of test classes and all test classes extend this BaseTest class. This is one of the main features of Object Oriented Design (OOD) “Inheritance“. The code of BaseTest is shown below.

In this class, we declared the driver and wait variables. These are used by all test classes. Also, we wrote @BeforeClass setup method and @AfterClass teardown method. Again, all test classes use these methods.

In setup method, we create a ChromeDriver, then create 15 seconds of timeout time, and maximize the browser.

In teardown method, I closed all the browsers with driver.quit(); code line.

That’s all for the base test class. Now, let’s write the BasePage class.

BasePage class contains the common methods of all page classes such as click, writeText, readText etc. Here is it’s code.

Now, we can create our page classes. The first one is HomePage.java class. In this class we will declare:

  • Constructor 
  • Page Variables
  • Web Elements
  • Page Methods

We will have two methods, one of them opens homepage and the other one opens the login page. Here is the code.

The another page class in LoginPage.java class. We will have three methods. One of them does the login operation, the other ones are assertion methods. Checks the login messages as expected or not and set the test fail or pass. Here is the code of LoginPage.java class.

and finally, we can write our login test class. I will write two tests:

  • InvalidUsernameInvalidPassword()
  • EmptyUsernameEmptPassword()

In test classes:

  1. We need to instantiate the required Page classes
  2. Use the appropriate page methods
  3. Do the assertions

Here is the loginTests.java class:

Here is the pom.xml and TestNG.xml files.

And the test result is shown below 😉

We can improve this POM framework with Utility classes such as DesiredcapabilitiesManager, AJAXWaiter, Assertions, Listeners, Reporters, PropertyReaders, and so on. We will see the improved version of this in next articles.

Github Link: https://github.com/swtestacademy/POMJAVAExample

See you!
-Onur

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 59 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
selenium

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
allure

Test Automation Reporting with Allure and TestNG

By Onur Baskirt / Oct 21, 2017 / 15 Comments
Hi all, in my last article I explained how to integrate ExtentReports reporting framework with TestNG for shiny and beautiful test automation reports [link]. In this article, I will explain you another popular test reporting framework which is developed by...
selenium

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
selenium

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 16 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
selenium

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
swtest-techniques

8 Important Software Testing Techniques

By Onur Baskirt / Mar 30, 2017 / 2 Comments
In this article, I will explain important software testing techniques which help you during test and User Acceptance Testing (UAT) processes. Disclaimer: The knowledge in this article is based on my experiences, understanding, and observation. If you don't agree on...
By | 2017-10-22T23:43:24+00:00 September 9th, 2017|Selenium, Test Automation|16 Comments

About the Author:

Onur Baskirt

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey’s biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

16 Comments

  1. Ozan September 12, 2017 at 11:40 am - Reply

    Great article! In my opinion without technical background, POM is one of the hardest part to understand. Well written. Thanks!
    I’m waiting for the new articles about it

    • Onur Baskirt
      Onur Baskirt September 12, 2017 at 1:39 pm - Reply

      Thank you, Ozan. I will try to add new articles on an improved version of POM soon.

  2. Sargam Dhaliwal September 26, 2017 at 8:23 am - Reply

    Really good article.

  3. juan October 30, 2017 at 7:01 pm - Reply

    Hi,
    I have question about PAGE INSTANTIATIONS,
    Is it possible to create a page Instantiations method and call from each @Test to reach the variables and methods ?
    Thanks

  4. Shrikant November 10, 2017 at 8:24 pm - Reply

    Hi Onur,

    I have a question about the parameterized constructor here. In each page class you are creating a page constructor where you are passing the Webdriver driver and Webdriver wait and then calling a super method with driver and wait variables. So, on instantiating in the Login.test class, the control will flow to base test class first or base page class? could you please explain with a pictorial representation.

    Thanks

    • Onur Baskirt
      Onur Baskirt November 10, 2017 at 8:57 pm - Reply

      Hi Shrikant,

      Yes, you are right. In BaseTest class I created driver and wait and pass these variables to page classes in specific test classes. We started to send these variables from BaseTest class to Page classes. You understood well.

  5. kaan November 15, 2017 at 6:54 am - Reply

    Hi,

    I think If we return ‘this’ or ‘new’ instead of ‘void’, we can use method chaining and write more fluent test cases like

    homePage.goToN11().goToLoginPage().loginPage.loginToN11(“”,””);

    • Onur Baskirt
      Onur Baskirt November 15, 2017 at 7:01 pm - Reply

      Of course! You can chain the methods as you wrote 😉

  6. Eran December 4, 2017 at 10:48 am - Reply

    Hi Onur,
    I opened the project just as you said,
    but when when i try to run the test intellij says:
    java.lang.Exception: Method setup() should be static

    java.lang.Exception: Method teardown() should be static

    I understand i can change those methods and driver in basetest to static, but i’m trying to understand why in your code it’s not static,
    Thanks

    • Onur Baskirt
      Onur Baskirt December 4, 2017 at 12:26 pm - Reply

      I think you used/imported JUnit instead of TestNG. JUnit wants static setup() and teardown() methods.

      • Eran December 5, 2017 at 8:44 pm - Reply

        you are right, i am using junit instead of testng,
        so i changed the setup and teardown to be static and also the the driver in basetest to be static so the setup and teardown can use it

        • Onur Baskirt
          Onur Baskirt December 5, 2017 at 10:20 pm - Reply

          Great! Have a good testing and test automation Eran.

          • Eran December 6, 2017 at 12:33 pm

            Thank you,
            by the way is there a reason why you chose to set webdriver and wait as public and not protected?

  7. Eran December 6, 2017 at 12:31 pm - Reply

    Thank you!

Leave A Comment