Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

Singleton in Selenium Framework

๐Ÿ’ก What Is Singleton?

The Singleton Design Pattern means creating only one object (instance) of a class and reusing it wherever needed.

In Selenium automation, we use Singleton to make sure that only one WebDriver instance is created and used throughout the test execution.


⚙️ Why We Need Singleton in Selenium

If we create multiple WebDriver instances in the same test run:

  • ๐ŸชŸ Multiple browser windows may open unnecessarily

  • ๐Ÿ”„ Browser sessions may conflict

  • ⚠️ Tests may fail or behave unexpectedly

๐Ÿ‘‰ The Singleton pattern prevents this by allowing only one WebDriver instance to exist at a time.


๐Ÿงฉ How Singleton Works

1️⃣ The constructor is private, so no one can create objects directly.
2️⃣ A static method provides access to the single instance.
3️⃣ The same instance is reused wherever it’s needed.


๐Ÿงฑ Example: Singleton WebDriver Manager

package driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverSingleton {

    // Step 1: Create a private static instance variable
    private static WebDriver driver;

    // Step 2: Make the constructor private
    private DriverSingleton() { }

    // Step 3: Provide a public static method to get the driver instance
    public static WebDriver getDriver() {
        if (driver == null) {
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
        return driver;
    }

    // Step 4: Quit the driver and reset it to null
    public static void quitDriver() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }
}

How to Use It

public class TestExample {
    public static void main(String[] args) {
        // Get the same WebDriver instance
        WebDriver driver = DriverSingleton.getDriver();

        driver.get("https://example.com");
        System.out.println(driver.getTitle());

        // Close the browser
        DriverSingleton.quitDriver();
    }
}

๐Ÿš€ Benefits of Using Singleton

✅ Only one browser instance per test run
✅ Easy to manage WebDriver lifecycle
✅ Saves memory and resources
✅ Reduces code duplication


⚠️ Limitations

❌ Not suitable for parallel testing (multiple threads need separate driver instances)

๐Ÿ‘‰ In that case, use ThreadLocal for thread safety — You can refer below link to the detailed explanation here: https://softwaretestingbugblab.blogspot.com/2025/10/ensuring-thread-safety-in-parallel-test.html


๐Ÿงพ In Short:

The Singleton Design Pattern in Selenium ensures that only one WebDriver instance is created and reused across the entire test execution — making your framework cleaner, efficient, and easy to maintain.

Comments

Popular posts from this blog

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java