Utilizing Java's Genetics for Page Object Models in the Scope of Automation Testing

A Brief Review on the Goals of Web-Based Automation

While there are many languages to use as a framework for a Automation Engineer, Java offers a strong paradigm of Object Oriented Programming (OOP) as a toolkit for both seasoned and beginning programmers. As part of making Automation Tests, it is important for a team of software developers to be able to accurately model the functionality of the application that is to be tested. For the purposes of illustrating this concept, we will be working on a Web Based Application to better highlight these principles.

As it has been covered in a variety of topics with developers who have shared their experiences and resources with up and coming engineers, one of the core principles of using an OOP Language like Java is to model the behavior of the application in a way that can be understood among developers. For web-based applications, the most obvious implementation is to utilize what is commonly referred to as the Page Object Model (POM), which has its used within an Automation based project:

Utilizing a POM for the purposes of Automation Testing can be as strong as the underlying framework implemented for a java class. Ideally Page Objects has two common characteristics: having variables that act as web elements within the webpage, and the methods which the Page Objects call upon that define the underlying behavior of utilizing that page. It certainly works well for the purposes of getting exposure to Java programming in the mindset of automation, but it is important to remember that one of the core concepts of building a code library is implementing robust functionality that can be reused for future projects.

Fortunately for those that are familiar with the advanced features of Java, we can utilize a very powerful feature of the programming language that can be implemented on a basic level to understand it's utility for the Automation Engineer. For this, we will turn to Generics.

A Brief Insight to Generic Programming and Implementation

Whether or not Java Generics is familiar with veterans or completely foreign to beginners, Generics have been a feature of Java since JDK 5.0, and it refers to defining types and/or methods in a more "generic" manner. In order to make a feature more generic, we invoke a Type Parameter that gives definitions to our classes, interfaces, methods, etc. that adds a layer of abstraction. A succinct statement on Java Generics boils down to:

Java Generics are a language feature that allows for definition and use of generic types and methods.

A very basic example of a generics implemented, starts with a class ExampleClass with a defined constructor that utilizes ExampleClass<E> where the class uses type parameter E in creating the class instance. The type "E" is called a type argument, and allows classes to instantiate as a parameterized type denoted by "E." A basic example would be LinkedList<Integer> , that defines a linked list that can hold Integer objects.

Why would programmers be interested in utilizing generics? As some may have experienced, often times while writing and exciting new code we might hit an unexpected run time error because we didn't account for instances where classes are making function calls that don't belong to the class or trying to assign values to the wrong objects. Generics helps enforces type safety on the Compiler level in order to prevent new bugs introduced that might have been missed. The robustness of the code becomes even more apparent on larger applications, as we can write classes with type parameters to further drive the behavior of the source code.

Defining the behavior of classes, using Generic Programming in this context, can be used to enhance the structure of the Page Object Models we create by adding clever functionality that will have the effect of code stability and readability rolled into one! The diagram below will illustrate how the POM framework can be modified to include more robust features:

Setting Up the Automation Example

Prerequisites

Prerequisites for the Java project will require the use of the Java tool Maven for building and managing the automation project (Please refer to the wiki Topic InstallingMaven for further guidance on installing maven on your specified computer OS). Once you have followed the instructions, on the command line for the computer execute the following statement that will generate the Maven version installed:

> mvn --version

Once maven has been installed, the following dependencies will be saved in the pom.xml file:

  • Selenium for Java
  • Web Driver Manager
  • TestNG
  • Cucumber Core
  • Cucumber Java
  • Cucumber TestNG

The following Ultra Guide resources will help with installing the dependencies or provide a refresher on utilizing the dependancies covered in this project:

Any robust IDE that offers Maven/Java project installations will be required; while Eclipse and Intellij IDEA are among the most popular and driven primary for Java development, there is no means a "right" IDE (unless a project has specific requests!)

[For this tutorial, the author has written and maintained all the code within VS Code]

Writing Up the Java Classes

Using the TestNG Framework for Automation Tests

Now that we have defined a plethora of POM classes that we can use to mimic the webpages to text, let us go over the basics of utilizing the TestNG Framework to full capacity:
  • A textng.xml file should be written out, which can allow for parameters to help perform cross browser testing as needed.
  • Each of our tests should be contained within a class dedicated to testing that webpage.
  • Before and After annotations should be used to help set up and close our connections to the web browser.

I will cover the textng.xml file later since the file also accounts for Cucumber (which we will go into detail later). For now, here is an example of the payoff we get when utilizing generics in the POM framework:

We can see here in the file that for most of these method calls, each method returns the appropriate instance of the class that the page instance is accessing. The functionality of the test is easy to read and implement after considerations have been made with the POM framework.

Using the Cucumber Framework for Automation Tests

If Gherkin Scenarios and Cucumber Behavior Driven Development is favored for a testing framework, then the following resources will be needed for this integration with TestNG:
  • A textng.xml file should be written out, which can call the Runner class defined by the user
  • A Feature file that defines the scope of testing the application
  • A Step Definitions Java file that will define the implementation of the Feature file for testing

Going over the principles of Cucumbr framework, we need to write out a Freature file that we can use to drive the behavior of the tests being conducted. An example Feature file will look like this:

Define the scenario with Given,When, and Then statements to guide each Scenario to be used for the tests. Once the textng.xml file is configured to run the TestRunner class, we can then run Maven Test to generate our methods for the StepDefinitions class. Here is what the file should look like based on the feature file shared earlier:

Just like in the TestNG example, functionality of the test is easy to read and implement, improving the overall structure of this Automation project. All we need now is to set up the TestRunner Java file and it should be ready to run the test.

Setting up the testng.xml file for the project

With this automation project, it is up to personal preference which testing framework is used for testing purposes. Fortunately with the dependencies installed, TextNG and Cucumber driven tests can be both executed, using a structure similar to the provided testng.xml file:

With the testng.xml file configured for out automation project, we're able to automate and run each of the tests as needed. [Cross browser support still needs to be explored for cucumber framework]

Generating Test Reports

Having a way to generate reports is always helpful in the scope of Automation projects, especially for the clients that would like to see meaningful data to indicate progress. While there are a multitude of default methods in testing frameworks to make reports, turning to maven once again to access advanced dependencies and plugins aids in the facility of generating more powerful reports that can be viewed and offer a clearer picture of the testing progress. For this project the Surefire Plugin and the Maven Cucumber Reporting Plugin can be used for the Test NG and Cucumber tests; for the tutorial purposes we will focus on generating more in-depth reports with the Cucumber Reporting framework specifically designed for Maven projects:

Once the Plugins are set up as shown above, then we can use the Verify command for the maven project to run the tests and generate the reports as needed within HTML files, located in the specified directory in the "!outputDirectory" tags. Here are two examples of HTML files generated by the Plugin that helps showcase the overall status of the run committed:

Closing Thoughts

Hopefully this tutorial will give a greater insight as to how Java Generics can be applied to an Automation Project that enables robust features that can be implemented. With good design of core classes it is easy to see how powerful enhancing the Page Object Model becomes critical on large scale projects.

For those that wish to experiment more with the base code, the entire code base for the project and necessary directories is currently hosted on a github repository. A git installation is required to access the github repository if you wish to clone it, so open the command line on the target directory and execute the following statement:

> git clone https://github.com/austin-ultratesting/JavaUICapstoneProject OPTIONAL_DIRECTORY_NAME

Sources used for building the Wiki Topic

Page Object Model with JAVA Generics

Angie Jones' guide on Date Picker Elements

TestNG Documentation

git Home Page

-- AustinBell - 19 Feb 2021

This topic: Automation > WebHome > ApplyingJavaGenericsandPageObjectModelsinSeleniumBasedTests
Topic revision: 19 Feb 2021, AustinBell
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co