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.chrome.ChromeBrowser;
7   import com.atlassian.webdriver.browsers.firefox.FirefoxBrowser;
8   import com.atlassian.webdriver.browsers.ie.IeBrowser;
9   import com.gargoylesoftware.htmlunit.BrowserVersion;
10  import org.openqa.selenium.WebDriver;
11  import org.openqa.selenium.firefox.FirefoxDriver;
12  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  import java.util.regex.Matcher;
17  import java.util.regex.Pattern;
18  
19  /**
20   * Checks the System property webdriver.browser to see what browser driver to return. Defaults to
21   * firefox.
22   */
23  public class WebDriverFactory
24  {
25      private static final Logger log = LoggerFactory.getLogger(WebDriverFactory.class);
26      private static final Pattern browserPathPattern = Pattern.compile("^([A-Za-z0-9_.-]+):path=(.*)$");
27  
28      private WebDriverFactory() {}
29  
30      public static AtlassianWebDriver getDriver()
31      {
32          return getDriver(null);
33      }
34  
35      public static String getBrowserProperty()
36      {
37          return System.getProperty("webdriver.browser", "firefox");
38      }
39  
40      public static Browser getBrowser()
41      {
42          String browserProperty = getBrowserProperty();
43  
44          return getBrowser(browserProperty);
45      }
46  
47      public static Browser getBrowser(String browserProperty)
48      {
49          if (RemoteWebDriverFactory.matches(browserProperty))
50          {
51              return RemoteWebDriverFactory.getBrowser(browserProperty);
52          }
53  
54          Matcher matcher = browserPathPattern.matcher(browserProperty);
55  
56          if (matcher.matches())
57          {
58              browserProperty = matcher.group(1);
59          }
60  
61          return Browser.typeOf(browserProperty);
62      }
63  
64      public static AtlassianWebDriver getDriver(BrowserConfig browserConfig)
65      {
66          WebDriver driver;
67          String browserPath = null;
68  
69          String BROWSER = getBrowserProperty();
70  
71          if (RemoteWebDriverFactory.matches(BROWSER))
72          {
73              return RemoteWebDriverFactory.getDriver(BROWSER, browserConfig);
74          }
75  
76          Matcher matcher = browserPathPattern.matcher(BROWSER);
77  
78          if (matcher.matches())
79          {
80              BROWSER = matcher.group(1);
81              browserPath = matcher.group(2);
82          }
83  
84          Browser browserType = Browser.typeOf(BROWSER);
85  
86          switch (browserType)
87          {
88              case FIREFOX:
89  
90                  if (browserPath == null && browserConfig != null)
91                  {
92                      driver = FirefoxBrowser.getFirefoxDriver(browserConfig);
93                  }
94                  else if (browserPath != null)
95                  {
96                      driver = FirefoxBrowser.getFirefoxDriver(browserPath);
97                  }
98                  else
99                  {
100                     driver = FirefoxBrowser.getFirefoxDriver();
101                 }
102                 break;
103 
104             case CHROME:
105 
106                 if (browserPath == null && browserConfig != null)
107                 {
108                     driver = ChromeBrowser.getChromeDriver(browserConfig);
109                 }
110 
111                 else if (browserPath != null)
112                 {
113                     driver = ChromeBrowser.getChromeDriver(browserPath);
114                 }
115                 else
116                 {
117                     driver = ChromeBrowser.getChromeDriver();
118                 }
119                 break;
120             
121             case IE:
122                 driver = IeBrowser.createIeDriver(browserPath, browserConfig);
123                 break;
124 
125             case HTMLUNIT_NOJS:
126                 driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24);
127                 ((HtmlUnitDriver) driver).setJavascriptEnabled(false);
128                 break;
129 
130             case HTMLUNIT:
131                 driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24);
132                 ((HtmlUnitDriver) driver).setJavascriptEnabled(true);
133                 break;
134 
135             case IPHONE_SIMULATOR:
136                 throw new UnsupportedOperationException("iPhone simulator is no longer a supported Browser Type. " +
137                         "Use remote iPhone driver instead");
138 
139             case IPHONE:
140                 // iPhones can only be driven via a RemoteWebDriver
141                 throw new RuntimeException("iPhone driver must be configured with a url parameter");
142 
143             case IPAD:
144                 // iPads can only be driven via a RemoteWebDriver
145                 throw new RuntimeException("iPad driver must be configured with a url parameter");
146 
147             case ANDROID_EMULATOR:
148                 throw new UnsupportedOperationException("Android emulator is no longer a supported Browser Type. " +
149                         "Use remote Android driver instead");
150 
151             case ANDROID:
152                 // Android can only be driven via a RemoteWebDriver
153                 throw new RuntimeException("Android driver must be configured with a url parameter");
154 
155             case SAFARI:
156                 throw new UnsupportedOperationException("Safari is not a supported Browser Type");
157 
158             case OPERA:
159                 throw new UnsupportedOperationException("Opera is not a supported Browser Type");
160 
161             default:
162                 log.error("Unknown browser: {}, defaulting to firefox.", BROWSER);
163                 browserType = Browser.FIREFOX;
164                 driver = new FirefoxDriver();
165         }
166 
167         BrowserUtil.setCurrentBrowser(browserType);
168 
169         return new DefaultAtlassianWebDriver(driver, browserType);
170     }
171 }