问题
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.
- 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