View Javadoc

1   package com.atlassian.webdriver;
2   
3   import com.atlassian.browsers.BrowserConfig;
4   import com.atlassian.pageobjects.browser.Browser;
5   import com.atlassian.pageobjects.util.BrowserUtil;
6   import org.openqa.selenium.Capabilities;
7   import org.openqa.selenium.firefox.FirefoxDriver;
8   import org.openqa.selenium.remote.DesiredCapabilities;
9   import org.openqa.selenium.remote.RemoteWebDriver;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import java.net.MalformedURLException;
14  import java.net.URL;
15  import java.util.regex.Matcher;
16  import java.util.regex.Pattern;
17  
18  /**
19   * Handles connecting to a web driver server remotely.
20   *
21   * @since 2.1.0
22   */
23  class RemoteWebDriverFactory
24  {
25      private static final Logger log = LoggerFactory.getLogger(WebDriverFactory.class);
26      private static final Pattern remoteBrowserPathPattern = Pattern.compile("^([A-Za-z0-9_.-]+):url=(.*)$");
27  
28      public static boolean matches(String browserProperty)
29      {
30          return remoteBrowserPathPattern.matcher(browserProperty).matches();
31      }
32  
33      public static Browser getBrowser(String browserProperty)
34      {
35          Matcher matcher = remoteBrowserPathPattern.matcher(browserProperty);
36  
37          if (!matcher.matches())
38          {
39              // this shouldn't happen anyway.
40              log.warn("Cannot find a url to connect to with a RemoteWebDriver. Falling back to using a local FirefoxDriver instead");
41              return Browser.FIREFOX;
42          }
43          Browser browserType = Browser.typeOf(matcher.group(1));
44          return browserType;
45      }
46  
47      public static AtlassianWebDriver getDriver(String browserProperty, BrowserConfig browserConfig)
48      {
49          Matcher matcher = remoteBrowserPathPattern.matcher(browserProperty);
50  
51          if (!matcher.matches())
52          {
53              // this shouldn't happen anyway.
54              log.warn("Cannot find a url to connect to with a RemoteWebDriver. Falling back to using a local FirefoxDriver instead");
55              return new DefaultAtlassianWebDriver(new FirefoxDriver(), Browser.FIREFOX);
56          }
57  
58          Browser browserType = Browser.typeOf(matcher.group(1));
59          final String serverUrlString = matcher.group(2);
60          URL serverUrl = null;
61          try
62          {
63              StringBuilder sb = new StringBuilder(serverUrlString);
64              if (!serverUrlString.contains("wd/hub"))
65              {
66                  if (!serverUrlString.endsWith("/")) {
67                      sb.append("/");
68                  }
69  
70                  sb.append("wd/hub");
71              }
72  
73              serverUrl = new URL(sb.toString());
74          }
75          catch (MalformedURLException e)
76          {
77              log.error("Invalid url provided: '{}', defaulting to http://localhost:4444.", serverUrlString);
78              try
79              {
80                  serverUrl = new URL("http://localhost:4444/wd/hub");
81              }
82              catch (MalformedURLException e1)
83              {
84                  // this shouldn't happen ignore
85              }
86          }
87  
88          final Capabilities capabilities;
89          switch (browserType)
90          {
91              case FIREFOX:
92                  capabilities = DesiredCapabilities.firefox();
93                  break;
94              case CHROME:
95                  capabilities = DesiredCapabilities.chrome();
96                  break;
97              case IE:
98                  capabilities = DesiredCapabilities.internetExplorer();
99                  break;
100             case HTMLUNIT:
101                 capabilities = DesiredCapabilities.htmlUnit();
102                 break;
103             case IPHONE:
104                 // iPhone must have iWebDriver app installed
105                 capabilities = DesiredCapabilities.iphone();
106                 break;
107             case IPAD:
108                 // iPad must have iWebDriver app installed
109                 capabilities = DesiredCapabilities.ipad();
110                 break;
111             case ANDROID:
112                 // android must have android-server.apk installed and running
113                 capabilities = DesiredCapabilities.android();
114                 break;
115             case SAFARI:
116                 throw new UnsupportedOperationException("Safari is not a supported Browser Type");
117             case OPERA:
118                 throw new UnsupportedOperationException("Opera is not a supported Browser Type");
119             default:
120                 log.error("Unknown browser: {}, defaulting to firefox.", browserType);
121                 capabilities = DesiredCapabilities.firefox();
122         }
123 
124         BrowserUtil.setCurrentBrowser(browserType);
125 
126         RemoteWebDriver driver = new RemoteWebDriver(serverUrl, capabilities);
127 
128         return new DefaultAtlassianWebDriver(driver, browserType);
129     }
130 }