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

Time:11-26

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 :)

CodePudding user response:

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();

        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.

  • Related