自动化测试-selenium初始化Driver参考

涉及到的工具:

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();
}

转载于:https://www.cnblogs.com/crazytester/p/7611670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值