How to get instance of loaded FirefoxDriver? How to get instance of loaded FirefoxDriver? selenium selenium

How to get instance of loaded FirefoxDriver?


You can have a class with a static part. You can initialize your driver in this class (that have to be static too). After that you can extends your test classes with this class in order to have you driver instance everywhere.

public class Context{    protected static WebDriver driver;    static {        driver = new FirefoxDriver();        //Do whatever you want.    }}public class MyTestClass extends Context{    @Test    public void test1(){        driver.something();    }}


public class DriverBuilder {    private WebDriver driver;        DriverBuilder() {        try {            driver = new FirefoxDriver();        } catch (Exception e) {            throw new ExceptionInInitializerError(e);        }    }       public WebDriver getDriver()     {        return driver;    }}

And use the same WebDriver instance whereever required as below:

WebDriver driver = DriverBuilder.getDriver();