Page Object Model is one of the most used test automation framework pattern and in this tutorial I will explain you this test automation design pattern. 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 to 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 one of the most popular Turkish e-commerce site 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.

It is always better to start with requirements, features, or in other terms our tests. It is like a top-down approach and this approach helps you not to make too many mistakes during creating your test automation framework. Thus, first I will start to create our LoginTest class. Our login scenarios are as follows:

  • InvalidUsernameInvalidPassword()
    • Given I am at Home Page
    • When I go to Login Page
    • And I try to login the website with invalid username and password
    • Then I see valid error messages
  • EmptyUsernameEmptPassword()
    • Given I am at Home Page
    • When I go to Login Page
    • And I try to login the website with empty username and password
    • Then I see valid error messages

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 class:

We have a lot of reds line in this class, we need to start to resolve them until our project will not have any red lines and problems. Let’s start with the BaseTest class.

BaseTest class 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 variable. This is used by all test classes. Also, we wrote @BeforeClass setup method and @AfterClass teardown method. Again, all test classes use these methods.

In the setup method, we create a ChromeDriver and maximize the browser.

In the 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, assertEquals etc. Here is it’s code.

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

  • Constructor 
  • Page Variables
  • Web Elements
  • Page Methods

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

Another page class in 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 class.

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

When we run our project, we will get below test results 😉

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 the next articles. You can reach the source code of the project on swtestacademy POM JAVA Example Github Page.

Selenium Webdriver Tutorial Series

See you!