Singleton Design Pattern In Selenium WebDriver

Hello Folks,

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:

  1. Declare constructor of class as private so that no one instantiate class outside of it.
  2. Declare a static reference variable of class. Static is needed to make it available globally.
  3. 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?

  1. Keep track of same driver instance throughout execution.
  2. DBMS connectivity.
  3. Loading external files like properties, excel etc once rather than loading again and again.
  4. Logger.

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, 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.

22 thoughts on “Singleton Design Pattern In Selenium WebDriver

  1. well explained, Thank you so much Amod Mahajan. if possible could you please upload sample code of Singleton design pattern entire sample framework.

  2. In Page object model, we implement page chaining concept, where class extends (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?

    1. 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.

    1. 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.

  3. 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 ?

  4. 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 ?

    1. That depends on your architecture. If you have a mechanism of singleton pattern, you can have only instance of each page class.

Leave a Reply

Your email address will not be published. Required fields are marked *