Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Singleton Design Pattern In Selenium WebDriver

Posted on 03/16/2025 By admin

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:

Output:

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.

SingletonBrowserClass.java:

LoadURL.java:

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. #ThanksForReading

#HappySelenium

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO
Next Post: REST Assured Tutorial 51 – How To Retrieve and Assert Content-Type of Response in Rest Assured

Related Posts

CombinedCSS – Make Selenium Easy Uncategorized
getAttribute Uncategorized
October 19, 2018 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
May 10, 2019 – Make Selenium Easy Uncategorized
ReducedStackTrace – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark