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