0

嗨,我有以下设置与我当前的框架,

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

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

有什么建议么 ?

PS:请原谅我遵循的不良代码约定

4

2 回答 2

7

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

为什么要切换浏览器?

于 2013-07-25T10:31:56.287 回答
0

您可能希望看到针对您的情况的不同方法。我相信,如果您必须使用 2 个浏览器,您很可能会尝试将一些信息从一个浏览器传递到另一个浏览器。这是我的看法:

    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 currentBrowser, firefoxInstance chromeInstance;
        firefoxInstance = new FirefoxDriver();
        chromeInstance= new ChromeDriver();
        currentBrowser = firefoxInstance; //if you want start out with Firefox

        currentBrowser()
        {
            return currentBrowser; 
        }

        switchBrowser(Cookies passingInfo) //passingInfo could be like cookies but also just current page etc...
        {
            if(currentBrowser==firefoxInstance)
            {
                chromeInstance.cookies()=passingInfo; // this is definitely not the correct way of passing cookies in Selenium but you get my point
                currentBrowser=chromeInstance;

            }
            else
            {
                firefoxInstance.cookies()=passingInfo;
                currentBrowser=firefoxInstance;
            }
        }
    }

当然,这样做的方法不止一种,这取决于你的最终目标是什么。但请记住,根据连接到它们的浏览器的用户代理,某些网站的设计会有所不同,这可能会导致您的代码崩溃(就像我 2 分钟前刚刚经历的那样。)如果可以的话,我建议坚持使用一个网络浏览器。

PS:请原谅我使用你糟糕的代码约定:)

于 2013-07-25T22:00:57.437 回答