Today I applied singleton design pattern for Selenium chrome
driver. I wanted to share the same instance of the driver across different steps.
The pattern appeared most promising to me, Luckily the pattern Singleton is the
one that fill my needs
Under The hood this
time I wrote some C# code to apply the idea.
The variable Instance holds the single instance of the class.
The constructor is declared private.
This provide me the global access and a single instant of
the web driver.
Love it as I would always be using one instant of it.
using System;
namespace carsales
{
public class Driver
{
private static IWebDriver Instance;
private Driver()
{
}
public static IWebDriver Int()
{
if (Instance == null)
Instance = new ChromeDriver();
return Instance;
}
public static void Navigate(string url)
{
Instance.Navigate().GoToUrl(url);
}
public static void Close()
{
Instance.Close();
}
}
}