...4import com.google.common.collect.ImmutableMap;5import java.io.File;6import java.io.IOException;7import org.openqa.selenium.WebDriverException;8import org.openqa.selenium.remote.service.DriverService;9import org.openqa.selenium.remote.service.DriverService.Builder;10public class ChromeDriverService11 extends DriverService12{13 public static final String CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver";14 public static final String CHROME_DRIVER_LOG_PROPERTY = "webdriver.chrome.logfile";15 public static final String CHROME_DRIVER_VERBOSE_LOG_PROPERTY = "webdriver.chrome.verboseLogging";16 public static final String CHROME_DRIVER_SILENT_OUTPUT_PROPERTY = "webdriver.chrome.silentOutput";17 public static final String CHROME_DRIVER_WHITELISTED_IPS_PROPERTY = "webdriver.chrome.whitelistedIps";18 19 public ChromeDriverService(File executable, int port, ImmutableList<String> args, ImmutableMap<String, String> environment)20 throws IOException21 {22 super(executable, port, args, environment);23 }24 25 public static ChromeDriverService createDefaultService()26 {27 return (ChromeDriverService)((Builder)new Builder().usingAnyFreePort()).build();28 }29 30 public static class Builder31 extends DriverService.Builder<ChromeDriverService, Builder>32 {33 private boolean verbose = Boolean.getBoolean("webdriver.chrome.verboseLogging");34 private boolean silent = Boolean.getBoolean("webdriver.chrome.silentOutput");35 private String whitelistedIps = System.getProperty("webdriver.chrome.whitelistedIps");36 37 public Builder() {}38 39 public Builder withVerbose(boolean verbose)40 {41 this.verbose = verbose;42 return this;43 }44 45 public Builder withSilent(boolean silent)46 {47 this.silent = silent;48 return this;49 }50 51 public Builder withWhitelistedIps(String whitelistedIps)52 {53 this.whitelistedIps = whitelistedIps;54 return this;55 }56 57 protected File findDefaultExecutable()58 {59 return ChromeDriverService.findExecutable("chromedriver", "webdriver.chrome.driver", "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver", "http://chromedriver.storage.googleapis.com/index.html");60 }61 62 protected ImmutableList<String> createArgs()63 {64 if (getLogFile() == null) {65 String logFilePath = System.getProperty("webdriver.chrome.logfile");66 if (logFilePath != null) {67 withLogFile(new File(logFilePath));68 }69 }70 71 ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();72 argsBuilder.add(String.format("--port=%d", new Object[] { Integer.valueOf(getPort()) }));73 if (getLogFile() != null) {74 argsBuilder.add(String.format("--log-path=%s", new Object[] { getLogFile().getAbsolutePath() }));75 }76 if (verbose) {77 argsBuilder.add("--verbose");78 }79 if (silent) {80 argsBuilder.add("--silent");81 }82 if (whitelistedIps != null) {83 argsBuilder.add(String.format("--whitelisted-ips=%s", new Object[] { whitelistedIps }));84 }85 86 return argsBuilder.build();87 }88 89 protected ChromeDriverService createDriverService(File exe, int port, ImmutableList<String> args, ImmutableMap<String, String> environment)90 {91 try92 {93 return new ChromeDriverService(exe, port, args, environment);94 } catch (IOException e) {95 throw new WebDriverException(e);96 }97 }98 }99}...