1 package com.atlassian.webdriver.browsers.chrome;
2
3 import com.atlassian.browsers.BrowserConfig;
4 import com.google.common.collect.Maps;
5 import org.apache.commons.lang.StringUtils;
6 import org.openqa.selenium.chrome.ChromeDriver;
7 import org.openqa.selenium.chrome.ChromeDriverService;
8 import org.openqa.selenium.chrome.ChromeOptions;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.io.File;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Map;
16
17
18
19
20 public class ChromeBrowser
21 {
22 private static final Logger log = LoggerFactory.getLogger(ChromeBrowser.class);
23
24 private ChromeBrowser()
25 {
26 }
27
28 public static ChromeDriver getChromeDriver()
29 {
30 return new ChromeDriver();
31 }
32
33
34
35
36
37
38
39 public static ChromeDriver getChromeDriver(BrowserConfig browserConfig)
40 {
41 if (browserConfig != null)
42 {
43 final ChromeOptions options = new ChromeOptions();
44 options.setBinary(browserConfig.getBinaryPath());
45 addCommandLine(options);
46
47 final ChromeDriverService.Builder chromeServiceBuilder = new ChromeDriverService.Builder();
48 setChromeServicePath(browserConfig, chromeServiceBuilder);
49 setEnvironment(chromeServiceBuilder);
50 chromeServiceBuilder.usingAnyFreePort();
51 ChromeDriverService chromeDriverService = chromeServiceBuilder.build();
52 return new ChromeDriver(chromeDriverService, options);
53 }
54
55
56 return getChromeDriver();
57 }
58
59 private static void setChromeServicePath(BrowserConfig browserConfig, ChromeDriverService.Builder chromeServiceBuilder)
60 {
61 if (browserConfig.getProfilePath() != null)
62 {
63 File profilePath = new File(browserConfig.getProfilePath());
64 File chromeDriverFile = new File(profilePath, "chromedriver");
65 if (chromeDriverFile.exists())
66 {
67 chromeServiceBuilder.usingDriverExecutable(chromeDriverFile);
68 }
69 }
70 }
71
72
73
74
75
76 private static void addCommandLine(final ChromeOptions options)
77 {
78 String[] switches = StringUtils.split(System.getProperty("webdriver.chrome.switches"), ",");
79 if(switches != null && switches.length > 0) {
80 List<String> switchList = Arrays.asList(switches);
81 log.info("Setting command line arguments for Chrome: " + switchList);
82 options.addArguments(switchList);
83 }
84 }
85
86
87
88
89
90
91 public static ChromeDriver getChromeDriver(String browserPath)
92 {
93 if (browserPath != null)
94 {
95 final ChromeOptions options = new ChromeOptions();
96 options.setBinary(browserPath);
97 return new ChromeDriver(options);
98 }
99 else
100 {
101
102 log.info("Browser path was null, falling back to default chrome driver.");
103 return getChromeDriver();
104 }
105 }
106
107
108
109
110
111 private static void setEnvironment(ChromeDriverService.Builder chromeDriverServiceBuilder)
112 {
113 Map<String, String> env = Maps.newHashMap();
114 if (System.getProperty("DISPLAY") != null)
115 {
116 env.put("DISPLAY", System.getProperty("DISPLAY"));
117 }
118 chromeDriverServiceBuilder.withEnvironment(env);
119 }
120
121 }