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!