Home > database >  How To Use Variable For Driver In C# Using Selenium
How To Use Variable For Driver In C# Using Selenium

Time:10-29

I want to use different IWebDrivers For My Method. For that I use different public IWebDrivers. So far no problem. However I would like to use a variable whenever I for example want to search for a specific element.

For Illustration: driver.Navigate.GoToUrl("https://www.google.com"); So, how would I know use a not yet defined variable? In my method I will use an if statement in connection with an else if statement, so depending on which if statement is used, a specific driver should be used.

For Illustration: if (...) a = driver1 else if (...) a = driver2. Since the driver would only defined whenever one of the if-statements or if-else statements has been initialized, it tells me that I can't use .Navigate with a. Is there a way to solve this? Otherwise I would have a lot of duplicate code

CodePudding user response:

If I understood your question correctly it may help to pack your drivers into a list and reference to the one you want. Something like this:

        ChromeDriver driver1 = new ChromeDriver();
        EdgeDriver driver2 = new EdgeDriver();

        IWebDriver[] drivers = new IWebDriver[2];
        drivers[0] = driver1;
        drivers[1] = driver2;

        int driverToUse;

        if (true)
        {
            driverToUse = 0;
        }
        else
        {
            driverToUse = 1;
        }

        drivers[driverToUse].Navigate().GoToUrl("https://www.google.com");

CodePudding user response:

The factory method pattern is a software design pattern that allows you to defer initialization of an object until runtime, where based on some condition you return different concrete implementations of an abstract class or interface. You might be able to use this pattern to decouple the initialization of specific web drivers from the code that needs to automate the browser.

Typically you need a class or just a method to make this decision. The example below uses a class, but you can put the CreateWebDriver method in any class that you want.

public class WebDriverFactory
{
    public IWebDriver CreateWebDriver(string driverType)
    {
        switch (driverType.ToLower())
        {
            case "firefox":
                return new GeckoDriver();
            case "chrome":
                return new ChromeDriver();
            case "edge":
                return new EdgeDriver();
            case "opera":
                return new OperaDriver();
            case "internet explorer":
                // fall through
            case "ie"
                return new InternetExplorerDriver();
            case "safari":
                return new SafariDriver();
        }
    }
}

To use this factory object:

var factory = new WebDriverFactory();

var driver = factory.CreateWebDriver("chrome");

driver.Navigate().GoToUrl("https://stackoverflow.com");

Since you do not have any code in your question, I cannot recommend a way to share this web driver object with the rest of your automation code. If you update your question with the code that you have, I can update my answer to figure out how to reuse this driver object in multiple tests or use cases.

  • Related