View Javadoc

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   * @since 2.1
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       * Configures a ChromeDriver based on the browserConfig
35       * @param browserConfig browser config that points to the binary path of the browser and
36       * optional profile
37       * @return A configured ChromeDriver based on the browserConfig passed in
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              setDefaultArgs(options);
46              addCommandLine(options);
47  
48              final ChromeDriverService.Builder chromeServiceBuilder = new ChromeDriverService.Builder();
49              setChromeServicePath(browserConfig, chromeServiceBuilder);
50              setEnvironment(chromeServiceBuilder);
51              chromeServiceBuilder.usingAnyFreePort();
52              ChromeDriverService chromeDriverService = chromeServiceBuilder.build();
53              return new ChromeDriver(chromeDriverService, options);
54          }
55  
56          // Fall back on default chrome driver
57          return getChromeDriver();
58      }
59  
60      /**
61       * Gets a chrome driver based on the browser path based in
62       * @param browserPath the path to the chrome binary to use for the chrome driver.
63       * @return A ChromeDriver that is using the binary at the browserPath
64       */
65      public static ChromeDriver getChromeDriver(String browserPath)
66      {
67          if (browserPath != null)
68          {
69              final ChromeOptions options = new ChromeOptions();
70              options.setBinary(browserPath);
71              return new ChromeDriver(options);
72          }
73          else
74          {
75              // Fall back on default chrome driver
76              log.info("Browser path was null, falling back to default chrome driver.");
77              return getChromeDriver();
78          }
79      }
80  
81      private static void setDefaultArgs(ChromeOptions options) {
82          // as of Chrome 30+ setuid based sandbox doesn't work on Linux due to permission issues
83          options.addArguments("--disable-setuid-sandbox");
84      }
85  
86      private static void setChromeServicePath(BrowserConfig browserConfig, ChromeDriverService.Builder chromeServiceBuilder)
87      {
88          if (browserConfig.getProfilePath() != null)
89          {
90              File profilePath = new File(browserConfig.getProfilePath());
91              File chromeDriverFile = new File(profilePath, "chromedriver");
92              if (chromeDriverFile.exists())
93              {
94                  chromeServiceBuilder.usingDriverExecutable(chromeDriverFile);
95              }
96          }
97      }
98  
99      /**
100      * Add command line options if provided via system property {@literal webdriver.chrome.switches}.
101      *
102      */
103     private static void addCommandLine(final ChromeOptions options)
104     {
105         String[] switches = StringUtils.split(System.getProperty("webdriver.chrome.switches"), ",");
106         if(switches != null && switches.length > 0) {
107             List<String> switchList = Arrays.asList(switches);
108             log.info("Setting command line arguments for Chrome: " + switchList);
109             options.addArguments(switchList);
110         }
111     }
112 
113     /**
114      * Sets up system properties on the chrome driver service.
115      * @param chromeDriverServiceBuilder the chrome driver service to set environment map on.
116      */
117     private static void setEnvironment(ChromeDriverService.Builder chromeDriverServiceBuilder)
118     {
119         Map<String, String> env = Maps.newHashMap();
120         if (System.getProperty("DISPLAY") != null)
121         {
122             env.put("DISPLAY", System.getProperty("DISPLAY"));
123         }
124         chromeDriverServiceBuilder.withEnvironment(env);
125     }
126 
127 }