Make Selenium Easy

How To Use Singleton Class to Manage Instance Variables in Automation Framework – Java

Introduction

In the previous post, we have seen how maximum beginners and experienced Selenium professionals apply inheritance with TestNG annotations while setup test scripts and end up with encountering NullPointerException. We have solved NullPointerException by converting instance variables to Static variables.

We can solve the above problem using a Singleton design pattern as well. Let’s learn about it.

Dependency version details

TestNG version used as below:-

Singleton Class

Let’s think of a scenario. A money bank may have many branches and many registered customers can withdraw or deposit or access there account at any time any place. Bank has a centralized common server where details of each customer are stored. If a new branch is opened it will also access data from the same server. If a brach is closed then also server will have customer’s data. In short, there is a common place where the state of data is stored.

We can consider an example of money withdrawal from ATM. If a customer tries to withdraw money simultaneously from two places, surely it will fail in one place because they also have a common place where data is kept updated. We can call that common place as a Singleton class.

When we develop a class in such a way that it can have the only instance at any time, is called a Singleton class. If a class is not instantiated then instantiate it and if the class is already instantiated return the same instantiated instance of the class. If we allow only one instance of any class that means we will have a shared object (somewhat how does a class variable or static variable works). This will solve our problem explained in the previous post. Variables initialized in TestNG annotated method will be stored in a common object for usage wherever we want.

Singleton class to store shared variables

Setup class with TestNG annotation

FirstTestNG class

SecondTestNG class

SecondTestNullPointerExceptionWithSingleton.xml

Output

You may ask in the above Singleton class creating variables in advance will be difficult and how can we save random variables. We can make out singleton class more advance with Map where will give user flexibility to save any data. This implementation even helps in sharing context among tests.

Singleton class with Map

Setup class

FirstTestNG class

SecondTestNG class

There is no change in TestNG xml.

Output

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “How To Use Singleton Class to Manage Instance Variables in Automation Framework – Java

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.