前一篇文章里面写的是用driver打开本地的Chrome, 在一个项目中,大家本地的Chrome可能版本不一定相同,所以最好用diver打开同一个Chrome,这个Chrome可以放在工程里面。
下面是具体的代码:
public class TestCaseBase {
private void setUpChrome() throws Exception{
File fileForDownload = new File(PROPERTIES_RESOURCES.getProperty("DownloadDir"));
String desiredFilePathForDownload = fileForDownload.getAbsolutePath(); //Chrome的下载文件的存放路径
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
File chromeDriver = new File("./lib/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath()); //设置ChromeDriver的property
HashMap<String, Object> contentPrefs = new HashMap<String, Object>();
contentPrefs.put("multiple-automati" + "c-downloads", 1);
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", desiredFilePathForDownload);
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("profile.default_content_settings", contentPrefs);
ChromeOptions options = new ChromeOptions();
File chromeBinary = new File("lib/Google Chrome/chrome.exe");
options.setBinary(chromeBinary.getAbsolutePath());
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
capability.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
capability.setCapability(ChromeOptions.CAPABILITY, options);
}
}