'Is there a way to pass the webdriver instance from one class to another in c# selenium?

I've been searching for a while and can't seem to find anything on c# about this, only java and other languages. Does anyone know how/if it is possible and is able to provide a simple example or point me the documentation available for me to read?

Very thankful :)



Solution 1:[1]

You can directly pass the driver instance via constructor like this;

public class Program
{
    private static void Main()
    {
        var driver = new ChromeDriver();

        var instance = new AnotherClass(driver);

        instance.DoSomething();

        Console.ReadKey(true);
    }
}

public class AnotherClass
{
    private readonly IWebDriver _driver;

    public AnotherClass(IWebDriver driver)
    {
        _driver = driver;
    }

    public void DoSomething()
    {
        _driver.Navigate().GoToUrl("https://www.facebook.com/");  
    }
}

Or you can store your driver in a property and make your main class static, so you can access it from any other class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1