我使用SharedDriver实现了cucumber-jvm picocontainer,并在本地运行良好。我想使用配置良好的Selenium Grid,但我不知道如何修改Shareddriver
类以便使用RemoteWebDriver
代替WebDriver
并连接到Selenium GRID。
new RemoteWebDriver(new("http://..../wd/hub"), capability);
无法正常工作,因为我需要抛出MalFormedException
而REAL_DRIVER
是一个静态字段。
有什么想法吗?谢谢!
public class SharedDriver extends EventFiringWebDriver {
private static final WebDriver REAL_DRIVER = WebDriverFactory.internetExplorerWebDriver();
private static final Thread CLOSE_THREAD = new Thread() {
@Override
public void run() {
REAL_DRIVER.close();
}
};
static {
Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}
public SharedDriver() {
super(REAL_DRIVER);
}
@Override
public void close() {
if(Thread.currentThread() != CLOSE_THREAD) {
throw new UnsupportedOperationException(
"WebDriver should not close!"
);
}
super.close();
}
@Before
public void deleteAllCookies() {
manage().deleteAllCookies();
}
@After
public void embedScreenshot(Scenario scenario) {
...
}
}
WebDriverFactory:
class WebDriverFactory {
static {
System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
}
static WebDriver internetExplorerWebDriver() {
DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
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);
}
答案 0 :(得分:1)
您可以将return语句包装到try..catch
块中,并在发生异常时返回null
。
class WebDriverFactory {
static {
System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
}
static WebDriver internetExplorerWebDriver() {
DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
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);
try {
return new RemoteWebDriver(new URL("http://www.google.com"), returnCapabilities);
} catch (MalformedURLException e) {
return null;
}
}
然后,检查REAL_DRIVER
值是否为null
。