Selenium webdriver:多个驱动程序

时间:2016-01-04 13:21:08

标签: java selenium selenium-webdriver webdriver

我使用了3个驱动程序firefox,chrome和IE进行自动化测试。

static WebDriver driver ;
    public static void main(String args[]) {
        try {

            driver = new FirefoxDriver();
            runTest(driver, "FireFox");

            //Chrome
            System.setProperty("webdriver.chrome.driver","E:/selinium_drivers/chromedriver.exe");
            driver = new ChromeDriver();
            runTest(driver, "Chrome");

            //IE
            System.setProperty("webdriver.ie.driver","E:/selinium_drivers/IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            runTest(driver, "IE");
            }

问题是第二个驱动程序在第一个驱动程序完成它的过程之前启动。如何在第一个驾驶员完成工作之前停止第二个驾驶员。

public static void runTest(WebDriver driver, String browserName) {
        try {
            testLogin(driver);
            testSignupC(driver);
            testSignUpCLogin(driver);
            driver.close();
        } catch(Exception ex) {
            //log stack trace
            //Alter(test failed in browser name)
        }
    }

1 个答案:

答案 0 :(得分:4)

你没有提到你的WebDriver版本;我的评论基于API 2.45。

首先,您应该拨打driver.quit()而不是close()

/**
 * Close the current window, quitting the browser if it's the last window currently open.
 */
void close();

/**
 * Quits this driver, closing every associated window.
 */
void quit();

其次,您应该在finally块中确保驱动程序关闭:

try {
    testLogin(driver);
    testSignupC(driver);
    testSignUpCLogin(driver);
} catch(Exception ex) {
    //log stack trace
    //Alter(test failed in browser name)
} finally {
    driver.quit();
}

此外,我会在quit()上使用try / catch包围WebDriverException,并将驱动程序设置为null,以防止自驱动程序初始化(startClient()以来重用它, startSession() ...)在构造函数中完成:

} finally {
    try {
        driver.quit();
    catch (WebDriverException e) {
        // log if useful else NO OP
    }
    driver = null;
}