0

I need to know how we can use the same instances of a Selenium Driver in different classes? So I have created an instance of the driver in a class, using the code :

IWebDriver driver = new ChromeDriver("C:/Users/Krishna/source/repos/G1ANT.Addon.YouTube/packages/Selenium.Chrome.WebDriver.83.0.0/driver");

So I need to use this instance in the new class, how could I do that?

Thank You

  • create a helper class and put the `IWebDriver` as a static property of that class. Use it else where – Beingnin Jul 17 '20 at 05:19

2 Answers2

0

The simplest approach is to put the driver in a static field of another class like below. Or you can use a singleton design pattern for returning the driver instance one and only once throughout the application

public class DriverHelper
{
    public static readonly IWebDriver Driver = new ChromeDriver("C:/Users/Krishna/source/repos/G1ANT.Addon.YouTube/packages/Selenium.Chrome.WebDriver.83.0.0/driver");   

}

Then access the driver like

var _driver = DriverHelper.Driver;
Beingnin
  • 2,288
  • 1
  • 21
  • 37
0
You need to create utils class for driver instance so you can ignore nullpoint exception.  i will share my utils class for driver instance you can use it (create package for utils classes inside you project folder)

    package utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

public class TestApp {
    private WebDriver driver;
    private static TestApp myObj;
    // public static WebDriver driver;
    utils.PropertyFileReader property = new PropertyFileReader();

    public static TestApp getInstance() {
        if (myObj == null) {
            myObj = new TestApp();
            return myObj;
        } else {
            return myObj;
        }
    }

    //get the selenium driver
    public WebDriver getDriver()
    {
        return driver;
    }

    //when selenium opens the browsers it will automatically set the web driver
    private void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    public static void setMyObj(TestApp myObj) {
        TestApp.myObj = myObj;
    }

    public void openBrowser() {
        //String chromeDriverPath = property.getProperty("config", "chrome.driver.path");
        //String chromeDriverPath = property.getProperty("config", getChromeDriverFilePath());
        System.setProperty("webdriver.chrome.driver", getChromeDriverFilePath());
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications");
        options.addArguments("disable-infobars");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void navigateToURL() {
        String url =property.getProperty("config","url");
        driver.get(url);
    }

    public void closeBrowser()
    {
        driver.quit();
    }

    public WebElement waitForElement(By locator, int timeout)
    {
        WebElement element = new WebDriverWait(TestApp.getInstance().getDriver(), timeout).until
                (ExpectedConditions.presenceOfElementLocated(locator));
        return element;
    }

    private String getChromeDriverFilePath()
    {
        // checking resources file for chrome driver in side main resources
        URL res = getClass().getClassLoader().getResource("chromedriver.exe");
        File file = null;
        try {
            file = Paths.get(res.toURI()).toFile();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
}
Note if you are using mac mention above chromedriver only 
Keep chrome driver in resource folder 

TestApp.getInstance().getDriver() -This is way use driver.


the 2 nd option is you can create global variable for driver instance 
 ---**static private WebDriver driver;**
Justin Lambert
  • 940
  • 1
  • 7
  • 13