如何在WebDriver中切换实例

Faz*_*azy 0 java firefox selenium webdriver

嗨我有我当前框架的设置,

ClassA
{
    //Which Receives Selenium WebDriver call the 'driver' object reference to manipulate the locators in UI

    public WebDriver get()
    {
        return MainClass.driver;
    }
}

MainClass
{
    public static Webdriver driver;
    method A()
    {
        //which uses Firefox instance and it is passed to ClassA to operate
    driver = new FirefoxDriver();
    }

    methodB()
    {
        //which creates new instance of Chrome
        driver = new ChromeDriver();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是,一旦我调用methodB()创建了Chrome的实例,但是一旦完成,我想恢复到在chrome运行之前可用或调用的firefox实例,但是我的方法是因为我指的是相同的webdriver对象旧的firefox引用被删除.

有什么建议 ?

PS:请原谅我遵循的错误代码约定

Akb*_*bar 7

最简单的解决方案是为FF和Chrome创建单独的对象.修改get方法以获取参数(browserType),然后返回correspoding对象.

你为什么要换浏览器?