自动化测试-selenium初始化Driver参考
Posted 疯狂的Tester
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化测试-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();
}
以上是关于自动化测试-selenium初始化Driver参考的主要内容,如果未能解决你的问题,请参考以下文章
python+selenium自动测试之WebDriver的常用API(基础篇一)
搞到你手软Selenium 自动化访问CSDN大牛的全部文章
Selenium WebUI自动化测试--PO中传递driver
Selenium3 Python3 Web自动化测试从基础到项目实战之二浏览器的不同设置