1. 程式人生 > >自動化測試-selenium初始化Driver參考

自動化測試-selenium初始化Driver參考

ref med ash 自動化 eve ima 測試 address get

涉及到的工具:

org.openqa.selenium.Proxy

org.openqa.selenium.chrome.ChromeDriver

org.openqa.selenium.chrome.ChromeOptions

代碼參考如下:

private void initDriver() throws MalformedURLException {
String browser = Env.brName;
Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://proxy-apsz.nslb.ad.moodys.net/mdyproxy.pac");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

if (StringUtils.equalsIgnoreCase(browser, "chrome")) {
String chromeSessionPath = System.getProperty("user.dir") + "\\chromeSessionId.txt";
String chromeLocalServer = System.getProperty("user.dir") + "\\chromelocalServer.txt";
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("disable-popup-blocking");
options.addArguments("--disable-extensions");
//Disable popup for whether save password
Map<String, Object> prefs=new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", bundle.getString("chrome_driver_path"));
if (Env.runInWorker) {
driver = new ChromeDriver(cap);
}else{
new MyChromeDriver(getServerAddressFromStorage(chromeLocalServer), getPreviousSessionIdFromStorage(chromeSessionPath), cap);
}
String sessionId = driver.getSessionId().toString();
saveSessionIdToFile(sessionId, chromeSessionPath);
HttpCommandExecutor cExecutor = (HttpCommandExecutor) driver.getCommandExecutor();
String sAddress = cExecutor.getAddressOfRemoteServer().toString();
saveServerAddressToFile(sAddress, chromeLocalServer);
} else if (StringUtils.equalsIgnoreCase(browser, "ie")) {
String ieSessionPath = System.getProperty("user.dir") + "\\ieSessionId.txt";
String ieLocalServer = System.getProperty("user.dir") + "\\ielocalServer.txt";
System.setProperty("webdriver.ie.driver", bundle.getString("ie_driver_path"));
if (Env.runInWorker) {
driver = new InternetExplorerDriver(cap);
}else{
new MyInternetExplorerDriver(getServerAddressFromStorage(ieLocalServer), getPreviousSessionIdFromStorage(ieSessionPath), cap);
}
String sessionId = driver.getSessionId().toString();
saveSessionIdToFile(sessionId, ieSessionPath);
HttpCommandExecutor cExecutor = (HttpCommandExecutor) driver.getCommandExecutor();
String sAddress = cExecutor.getAddressOfRemoteServer().toString();
saveServerAddressToFile(sAddress, ieLocalServer);
} else if (StringUtils.equalsIgnoreCase(browser, "ff")) {
ProfilesIni allprofiles = new ProfilesIni();
FirefoxProfile firefoxProfile = allprofiles.getProfile("default");
firefoxProfile.setPreference("app.update.enabled", false);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", System.getProperty("user.home") + "\\Downloads");
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-www-form-urlencoded,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/comma-separated-values,application/csv,application/octet-stream,application/pdf,application/excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.ms-excel,text/csv,application/zip,application/exe");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-www-form-urlencoded,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/comma-separated-values,application/csv,application/octet-stream,application/pdf,application/excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.ms-excel,text/csv,application/zip,application/exe");
cap.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
if (Env.runInWorker) {
driver = new FirefoxDriver(cap);
}else{
MyFirefoxDriver.ReuseFirefoxDriver(cap);
}
}else if(StringUtils.equalsIgnoreCase(browser, "edge")){
System.setProperty("webdriver.edge.driver", bundle.getString("edge_driver_path"));
driver = new EdgeDriver(cap);
}

initWaitTimeout();
}

自動化測試-selenium初始化Driver參考