博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动化测试-selenium初始化Driver参考
阅读量:4328 次
发布时间:2019-06-06

本文共 4441 字,大约阅读时间需要 14 分钟。

涉及到的工具:

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

你可能感兴趣的文章
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>