While researching about test automation frameworks, we come across this term many times. But before talking about Singleton Design Pattern, let's first understand what is Design Pattern.
A Design Pattern can be called as a solution to a commonly occurring problem. It is basically a template that can be used in different situations.
Singleton Design Pattern is one such approach which ensures that there will only be a single instance/object of a class. It is a Creational Design Pattern as it controls object creation.
This pattern involves development of a class in a way that it can only have one instance at any time. This is done when we need only one instance of a class across the whole project. Singleton class must return the same instance when the class is instantiated again. This approach also ensures better memory management by prohibiting object creation.
Implementation of Singleton Pattern -
There can be many approaches to create singleton class and implement the pattern, but all of them will follow the same rules as below -
No public constructors or a private constructor that restricts class's instantiation from other classes.
A private static variable, static makes it available globally.
A public static method that returns instance of the class, which should check if an instance of the class is already there before creating one.
How to use Singleton Design, specifically while doing Test Automation.
Let us understand this using a common step of Test Automation using Selenium Web Driver, as an example. (We will be using Java for the coding examples).
When we want to automate test cases for any website, first step is to initialize the Selenium web driver which is used to browse the website and interact with various controls on the pages.
Below is a basic approach to create a Selenium WebDriver instance and browse a website for testing.
Now, we create another test class to test another page on the same website. We follow the same steps to initialize the web driver and browse the website.
This all works fine when we run the tests for TestClass1 and TestClass2 separately.
But what if, we want to execute both test classes together (we will want to run the whole test suite , right!). When we do that, we will find two browsers open for both the test classes and we sure don't want that! We would want only one instance of the website open to run all the tests. But how do we reuse the webdriver/browser instance for all our test classes? Actually, what we need is a single instance of webdriver through out the test execution. This is where Singleton Design Pattern comes into picture.
We need to create a class which will take care of the webdriver instance following rules for Singleton Design Pattern and will make sure that there is only one instance of webdriver at any time. "DriverFactory" class in screenshot below solves the purpose and follows all the rules for Singleton Design.
There are no default/public constructors.
driver is private static variable which can't be accessed directly by any other classes.
getInstance is the public static method which returns the WebDriver object and makes sure that the webdriver instance is null before creating a new one.
Below is the usage of DriverFactory in the test file. Executing any number of such test classes will work on the same webdriver/browser.
Apart from above example, there can be many other use cases of Singleton Design Pattern in Test Automation like
Manage database connection, if database is connected already, why connect again!
Manage test data file readers, as they need to be used globally and multiple times through out the project.
Singleton Design Pattern is an important practice for Automation testing. It comes in handy when designing test frameworks where we need to control accesses.
Keep Exploring, Keep Learning!!