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 com.atlassian.webdriver.browsers.firefox.FirefoxBrowser;
7   import com.atlassian.webdriver.utils.WebDriverUtil;
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)
48      {
49          return getDriver(browserProperty, null);
50      }
51  
52      public static AtlassianWebDriver getDriver(String browserProperty, BrowserConfig browserConfig)
53      {
54          Matcher matcher = remoteBrowserPathPattern.matcher(browserProperty);
55  
56          if (!matcher.matches())
57          {
58              // this shouldn't happen anyway.
59              log.warn("Cannot find a url to connect to with a RemoteWebDriver. Falling back to using a local FirefoxDriver instead");
60              return new DefaultAtlassianWebDriver(FirefoxBrowser.getFirefoxDriver(), Browser.FIREFOX);
61          }
62  
63          Browser browserType = Browser.typeOf(matcher.group(1));
64          final String serverUrlString = matcher.group(2);
65          URL serverUrl = null;
66          try
67          {
68              StringBuilder sb = new StringBuilder(serverUrlString);
69              if (!serverUrlString.contains("wd/hub"))
70              {
71                  if (!serverUrlString.endsWith("/")) {
72                      sb.append("/");
73                  }
74  
75                  sb.append("wd/hub");
76              }
77  
78              serverUrl = new URL(sb.toString());
79          }
80          catch (MalformedURLException e)
81          {
82              log.error("Invalid url provided: '{}', defaulting to http://localhost:4444.", serverUrlString);
83              try
84              {
85                  serverUrl = new URL("http://localhost:4444/wd/hub");
86              }
87              catch (MalformedURLException e1)
88              {
89                  // this shouldn't happen ignore
90              }
91          }
92  
93          final DesiredCapabilities capabilities;
94          switch (browserType)
95          {
96              case FIREFOX:
97                  capabilities = DesiredCapabilities.firefox();
98                  break;
99              case CHROME:
100                 capabilities = DesiredCapabilities.chrome();
101                 break;
102             case IE:
103                 capabilities = DesiredCapabilities.internetExplorer();
104                 break;
105             case HTMLUNIT:
106                 capabilities = DesiredCapabilities.htmlUnit();
107                 break;
108             case IPHONE:
109                 // iPhone must have iWebDriver app installed
110                 capabilities = DesiredCapabilities.iphone();
111                 break;
112             case IPAD:
113                 // iPad must have iWebDriver app installed
114                 capabilities = DesiredCapabilities.ipad();
115                 break;
116             case ANDROID:
117                 // android must have android-server.apk installed and running
118                 capabilities = DesiredCapabilities.android();
119                 break;
120             case SAFARI:
121                 throw new UnsupportedOperationException("Safari is not a supported Browser Type");
122             case OPERA:
123                 throw new UnsupportedOperationException("Opera is not a supported Browser Type");
124             default:
125                 log.error("Unknown browser: {}, defaulting to firefox.", browserType);
126                 capabilities = DesiredCapabilities.firefox();
127         }
128 
129         final String capabilitiesStr = System.getProperty("webdriver.capabilities");
130         log.info("Loading custom capabilities " + capabilitiesStr);
131         DesiredCapabilities customCapabilities = WebDriverUtil.createCapabilitiesFromString(capabilitiesStr);
132         capabilities.merge(customCapabilities);
133 
134         BrowserUtil.setCurrentBrowser(browserType);
135 
136         RemoteWebDriver driver = new RemoteWebDriver(serverUrl, capabilities);
137 
138         return new DefaultAtlassianWebDriver(driver, browserType);
139     }
140 }