相关code:
public static WebDriver createInstance() throws MalformedURLException {
if (BROWSER == "Firefox"){
driverInstance = new FirefoxDriver();
driverInstance.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driverInstance.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driverInstance.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
} else if (BROWSER == "Safari"){
DesiredCapabilities capability = DesiredCapabilities.safari();
driverInstance = new RemoteWebDriver(new URL("http://192.168.61.138:5555/wd/hub"),capability);
capability.setBrowserName("safari");
capability.setPlatform(Platform.MAC);
capability.setVersion("6.0.2");
} else if (BROWSER == "Chrome"){
System.setProperty("webdriver.chrome.driver","D:\selenium\selenium-server-2.31.0\chromedriver.exe");
driverInstance = new ChromeDriver();
} else if (BROWSER == "iExplore"){
System.setProperty("webdriver.ie.driver", "D:\selenium\selenium-server-2.31.0\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driverInstance = new InternetExplorerDriver(capabilities);
} else if (BROWSER == "HtmlUnit" ) {
driverInstance = new HtmlUnitDriver(true);
}
return driverInstance;
}
3. 完成以上两步,差不多把所需的东西准备好了,接下来需要一步步开工了。首先,把底层框架中启动和停止server的代码修改为新的
1.0中需要启动server才能运行,而在2.0中如果不通过Remote来运行的话是不需要启动server的,这里提到的启动实际是实例化过程,这中间会启动浏览器,停止时会释放资源并关闭浏览器。
protected static void startdriver() {
if (driver == null ){
try {
driver = DriverInstance.createInstance();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void stopDriver() {
//stop driver;
if (driver != null) {
driver.quit();
if ( browser == "Firefox") {
closeFireFox();
} else if (browser == "Chrome") {
closeChromeDriver();
}
}
}