简体   繁体   中英

Selenium webdriver : Multiple drivers

I have used 3 drivers firefox, chrome and IE for automated testing.

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

The problem is the second driver is starting before the first driver complete it's process. How can i stop second driver till the first driver finish it work.

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

You didn't mention your WebDriver version; my comments are based on API 2.45.

First, you should call driver.quit() instead of 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();

Second, you should secure the driver shutdown in a finally block:

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

Additionally, I would surround quit() with a try/catch on WebDriverException and set driver to null in order to prevent its reuse since the driver initialization ( startClient() , startSession() ...) is done in the constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM