Cucumber picocontainer/SharedDriver doesn't handle browser instance properly

回眸只為那壹抹淺笑 提交于 2020-01-15 10:41:30

问题


I'm using cucumber-jvm picocontainer to share selenium driver between classes. I have ShareDriver and WebDriverFactory class.

My problem is the following: 1. If I run 2 test cases, the driver/browser instance is closed after the first test case, new browser instance is created and run the second one. I would like to use only 1 browser instance and run the tests, then close it.

  1. IEDriverServer.exe and one java.exe are stucked on task manager after the test, however the browser is closed. I need to kill them manually. Every run creates a new one from these tasks. I tried all ideas from stackoverflow, but none of them could solve this problem.

Thanks!

My SharedDriver class:

public class SharedDriver extends EventFiringWebDriver implements Startable {

    public SharedDriver() {
        super(WebDriverFactory.localInternetExplorerWebDriver());
    }

    @After
    public void embedScreenshot(Scenario scenario) {
        try {
            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
            System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {
        quit();
    }
}

My WebDriverFactory class:

class WebDriverFactory {
    static {
        System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
    }

    static WebDriver localInternetExplorerWebDriver() {
        DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
        System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
        //returnCapabilities.setCapability("nativeEvents", false);
        returnCapabilities.setCapability("requireWindowFocus", true);
        returnCapabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
        returnCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
        returnCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        returnCapabilities.setCapability("ignoreZoomSetting", true);
        return new InternetExplorerDriver(returnCapabilities);
    }
}

回答1:


The implementation of SharedDriver is not correct. You need a static webdriver field in the shareddriver class, create a shutdown thread, add this thread to the jvm shutdown hook. Use this one

If you wanna kill that too use this. Add this to the shutdown hook.Add it inside the run method of the thread after call to REAL_DRIVER.quit().



来源:https://stackoverflow.com/questions/49328239/cucumber-picocontainer-shareddriver-doesnt-handle-browser-instance-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!