Singleton Design Pattern In Selenium WebDriver
Today we will see implementation of Singleton design pattern in selenium webdriver.
Before we learn about implementation in Selenium webdriver, we must know about Singleton pattern.
Singleton Design Pattern:
When we develop a class in such a way that it can have only instance at any time, is called Singleton design pattern. It is very useful when you need to use same object of a class across all classes or framework. Singleton class must return the same instance again, if it is instantiated again.
To create a singleton class, we need to do following steps:
- Declare constructor of class as private so that no one instantiate class outside of it.
- Declare a static reference variable of class. Static is needed to make it available globally.
- Declare a static method with return type as object of class which should check if class is already instantiated once.
Look at below code snippet:
Explanation: When you run above program, you will get print “Object created.” only once while you have instantiated class twice. This is because of singleton pattern. It will not create object of class again if already initialized once.
How does Singleton pattern help in Selenium Webdriver?
- Keep track of same driver instance throughout execution.
- DBMS connectivity.
- Loading external files like properties, excel etc once rather than loading again and again.
So wherever you feel, you should have single instance of any class, you should use singleton design pattern. For example: If database connection is already established, you should not create new connection.
We will see implementation of singleton design pattern for keep track of driver.
Explanation: When you run LoadURL.java, you will see browser will be launched and url will be opened in same browser. We have instantiated two instance of class SingletonBrowserClass, but both give the same instance of driver.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.
22 thoughts on “Singleton Design Pattern In Selenium WebDriver”
Many thanks!! Amod, helpful for interview preparation
well explained, Thank you so much Amod Mahajan. if possible could you please upload sample code of Singleton design pattern entire sample framework.
In Page object model, we implement page chaining concept, where TestCase.java class extends BaseClass.java (i.e. Origin of the WebDriver instance) and that driver instance gets passed to other classes without creating new object. Can we say that, this is similar to Singleton class pattern?
Kind of. When you allow only single instance of any class that is called singleton class. This may be useful for establishing database connection, maintaining values of variables, reading env specific file etc.
How tom make use of this class at framework level?
In all scenarios where you want single instance of class. It may be a database connection, a json or excel or properties file reader, a WebDriver reference or shared variable.
I wanted to store this Singleton script in a different package but I am not able to inherit it. Also I cannot write main class as I am using TestNG for scripting Test cases. How to achieve combination of Single WebDriver class as Singleton and TestNG ?
I have existing framework in my company. I have a scenario like we go from Home page to Login page and then logout which will navigate back to Home page.
In this scenario, we have created a homePage1 object for HomePage class and when we revisit back to Home page after logout, we created another object called homePage2 for further validations.
My question here is, can’t we use the same homePage1 object again (when we revisit that page) without creating another object called homePage2 ?
That depends on your architecture. If you have a mechanism of singleton pattern, you can have only instance of each page class.
Can you please post the code for singleton design pattern?
very interesting , good job and thanks for sharing..
Hi, Can we run our test cases in parallel using singleton design pattern ?
You need to do some more coding using Thread and Singleton to achieve that.
Could you please throw some light on coding using Thread and Singleton to parallel testing.
It can be possible using ThreadLocal class in java.
Great bro….awaiting for other design patterns
Gud one, Thank for sharing.