相關(guān)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. 完成以上兩步,差不多把所需的東西準(zhǔn)備好了,接下來(lái)需要一步步開工了。首先,把底層框架中啟動(dòng)和停止server的代碼修改為新的
1.0中需要啟動(dòng)server才能運(yùn)行,而在2.0中如果不通過(guò)Remote來(lái)運(yùn)行的話是不需要啟動(dòng)server的,這里提到的啟動(dòng)實(shí)際是實(shí)例化過(guò)程,這中間會(huì)啟動(dòng)瀏覽器,停止時(shí)會(huì)釋放資源并關(guān)閉瀏覽器。
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();
}
}
}