Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading