View Javadoc

1   package com.atlassian.webdriver.browsers.ie;
2   
3   import com.atlassian.browsers.BrowserConfig;
4   import org.openqa.selenium.ie.InternetExplorerDriver;
5   import org.openqa.selenium.ie.InternetExplorerDriverService;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import javax.annotation.Nullable;
10  import java.io.File;
11  
12  /**
13   *
14   */
15  public final class IeBrowser
16  {
17      private static final Logger log = LoggerFactory.getLogger(IeBrowser.class);
18  
19      public static final String IE_SERVICE_EXECUTABLE = "IEDriverServer.exe";
20  
21      private IeBrowser()
22      {
23          throw new AssertionError("Don't instantiate me");
24      }
25  
26      public static InternetExplorerDriver createIeDriver(@Nullable String browserPath, @Nullable BrowserConfig config)
27      {
28          if (noBrowserPath(browserPath) && noServicePath(config))
29          {
30              return createDefaultDriver();
31          }
32          else
33          {
34              final InternetExplorerDriverService service = setBrowserExecutablePath(browserPath,
35                      setServiceExecutablePath(config, new InternetExplorerDriverService.Builder()))
36                      .usingAnyFreePort()
37                      .build();
38              return new InternetExplorerDriver(service);
39          }
40      }
41  
42      public static InternetExplorerDriver createDefaultDriver()
43      {
44          return new InternetExplorerDriver();
45      }
46  
47      private static boolean noBrowserPath(@Nullable String browserPath)
48      {
49          return browserPath == null;
50      }
51  
52      private static boolean hasServicePath(@Nullable BrowserConfig config)
53          {
54              return config != null && config.getProfilePath() != null;
55          }
56  
57      private static boolean noServicePath(@Nullable BrowserConfig config)
58      {
59          return !hasServicePath(config);
60      }
61  
62      private static InternetExplorerDriverService.Builder setServiceExecutablePath(BrowserConfig browserConfig, InternetExplorerDriverService.Builder builder)
63      {
64          if (hasServicePath(browserConfig))
65          {
66              File profilePath = new File(browserConfig.getProfilePath());
67              File ieDriverFile = new File(profilePath, IE_SERVICE_EXECUTABLE);
68              if (ieDriverFile.isFile())
69              {
70                  builder.usingDriverExecutable(ieDriverFile);
71              }
72          }
73          return builder;
74      }
75  
76      private static InternetExplorerDriverService.Builder setBrowserExecutablePath(String browserPath, InternetExplorerDriverService.Builder builder)
77          {
78              if (browserPath != null)
79              {
80                  // can't do much here, IE driver knows better where to look for IE
81                  log.warn("Non-null browser path configured for IE: '{}', but IEDriver does not support custom browser paths");
82              }
83              return builder;
84          }
85  
86  }