selenium操作chrome時的一些配置
private void getChromeDriver() {
Config config = new ConfigProvider().get();
Config configToUse = Optional.ofNullable(config).orElse(ConfigLoader.load());
prepareChromeDriver(configToUse);
final String osName = config.getString("os.name");
ChromeOptions chromeOptions = new ChromeOptions();
if (StringUtils.containsIgnoreCase(osName, "windows")) { // 本地Windows環境, 初始化local環境的chrome driver
logger.info("windows環境", osName);
chromeOptions.setBinary(configToUse.getString("webdriver.chrome.binary")); // windows下chrome地址
chromeOptions.addArguments("--profile-directory=target");
chromeOptions.addArguments("--user-data-dir=target\\");
} else { // FAT/UAT/PROD等Linux環境, 初始化Linux環境的chrome driver
logger.info("Linux環境", osName);
chromeOptions.setBinary("/usr/bin/google-chrome-stable"); // 配置chrome安裝地址
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
String proxyServer = qconfigService.getConfig("proxyHost") + ":" + qconfigService.getConfig("proxyPort"); //代理配置
chromeOptions.addArguments("--proxy-server=" + proxyServer);
}
chromeOptions.addArguments("--incognito"); // 開啟隱私模式
chromeOptions.addArguments("--lang=" + chineses); // 中文
chromeOptions.addArguments("-test-type", "--ignore-certificate-errors"); // 忽略證書錯誤
chromeOptions.addArguments("--no-sandbox");
// 新增chrome模擬器
// Map<String, String> mobileEmulation = new HashMap<String, String>();
// mobileEmulation.put("deviceName", "Galaxy S5");//"Nexus 5X");
// chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
String webDriverPath = System.getProperty("webdriver.chrome.driver");
ChromeDriverService driverService = new ChromeDriverService.Builder().usingDriverExecutable(new File(webDriverPath)).usingAnyFreePort().build();
try {
driverService.start();
} catch (IOException e) {
logger.error(e);
}
logger.info("webdriver.chrome.driver", webDriverPath);
ChromeDriver driver = new ChromeDriver(driverService, chromeOptions);
logger.info("webdriver.chrome.driver", webDriverPath);
}