Home > database >  How To Allow Only One ChromeDriver At A Time Using Selenium C#?
How To Allow Only One ChromeDriver At A Time Using Selenium C#?

Time:11-01

I currently have a simple method which initiate a ChromeDriver using the following method:

public void ChromeDriver()

{

ChromeDriverStart();

}

ChromeDriverStart() refers to a public method in which I set the options for the driver which I have set using IWebDriver. Now let's say I will initiate a driver named driverx, I only want it to be initiated if there is not already an instance of driverx active. So far I tried the following if-statement inside the method: if (driverx = null)

My goal is to not start another instance of driverx when calling the method ChromeDriver().

Whole Code:

public IWebDriver 1driver;
public IWebDriver 2driver;
public IWebDriver 3driver;
public IWebDriver 4driver;

public void DriverStart() 
{
    1driver = new ChromeDriver();
}

        public void Driver()
        {
            if (driver1 = null)
            {
                DriverStart();
            }
        }

CodePudding user response:

A different approach than other answers mentioned in the comments would be to use DI.

<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
    <PackageReference Include="Selenium.Support" Version="4.5.1" />
    <PackageReference Include="Selenium.WebDriver" Version="4.5.1" />
    <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="107.0.5304.6200" />
</ItemGroup>
// .NET 6 Console app

using IHost host = Create(args).Build();
using var scope = host.Services.CreateScope();
var provider = scope.ServiceProvider;

var driver1 = provider.GetRequiredService<IWebDriver>();
var driver2 = provider.GetRequiredService<IWebDriver>();

// If we use the scoped service only the one driver will be initiated.
// If we select to configure our service as transient then 
// both instances will be initiated

driver1.Navigate().GoToUrl(@"https://stackoverflow.com/");
driver2.Navigate().GoToUrl(@"https://stackoverflow.com/");

static IHostBuilder Create(string[] args) =>
         Host.CreateDefaultBuilder(args)
        .ConfigureServices((_, services) =>
        {
            // Uncomment one of them
            // services.AddScoped<IWebDriver, ChromeDriver>();
            // services.AddTransient<IWebDriver, ChromeDriver>();
        });

Of course this is a simple implementation of DI just for this demo. You will probably need to configure the drivers in different manner than your initial implementation. Maybe you could create a concrete class that injects IWebDriver and configure it according to your needs and then add it to DI as well.

Finally let me cut a long story short, using dependency injection you will have all your services into one location and also you can handle their lifecycles.

CodePudding user response:

Use the comparison operator to test for null:

if (driver1 == null)
{ //        ^^
    driver1 = new ChromeDriver();

The = operator is the assignment operator. The comparison operator uses two equals signs side-by-side: ==.

  • Related