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 com.gargoylesoftware.htmlunit.WebClient;
11 import org.openqa.selenium.WebDriver;
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
21
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 log.info("Loading RemoteWebDriverFactory driver " + BROWSER);
74 return RemoteWebDriverFactory.getDriver(BROWSER);
75 }
76
77 Matcher matcher = browserPathPattern.matcher(BROWSER);
78
79 if (matcher.matches())
80 {
81 BROWSER = matcher.group(1);
82 browserPath = matcher.group(2);
83 }
84
85 Browser browserType = Browser.typeOf(BROWSER);
86
87 switch (browserType)
88 {
89 case FIREFOX:
90
91 if (browserPath == null && browserConfig != null)
92 {
93 driver = FirefoxBrowser.getFirefoxDriver(browserConfig);
94 }
95 else if (browserPath != null)
96 {
97 driver = FirefoxBrowser.getFirefoxDriver(browserPath);
98 }
99 else
100 {
101 driver = FirefoxBrowser.getFirefoxDriver();
102 }
103 break;
104
105 case CHROME:
106
107 if (browserPath == null && browserConfig != null)
108 {
109 driver = ChromeBrowser.getChromeDriver(browserConfig);
110 }
111
112 else if (browserPath != null)
113 {
114 driver = ChromeBrowser.getChromeDriver(browserPath);
115 }
116 else
117 {
118 driver = ChromeBrowser.getChromeDriver();
119 }
120 break;
121
122 case IE:
123 driver = IeBrowser.createIeDriver(browserPath, browserConfig);
124 break;
125
126 case HTMLUNIT_NOJS:
127 driver = newHtmlUnitDriver(false);
128 break;
129
130 case HTMLUNIT:
131 driver = newHtmlUnitDriver(true);
132 break;
133
134 case IPHONE_SIMULATOR:
135 throw new UnsupportedOperationException("iPhone simulator is no longer a supported Browser Type. " +
136 "Use remote iPhone driver instead");
137
138 case IPHONE:
139
140 throw new RuntimeException("iPhone driver must be configured with a url parameter");
141
142 case IPAD:
143
144 throw new RuntimeException("iPad driver must be configured with a url parameter");
145
146 case ANDROID_EMULATOR:
147 throw new UnsupportedOperationException("Android emulator is no longer a supported Browser Type. " +
148 "Use remote Android driver instead");
149
150 case ANDROID:
151
152 throw new RuntimeException("Android driver must be configured with a url parameter");
153
154 case SAFARI:
155 throw new UnsupportedOperationException("Safari is not a supported Browser Type");
156
157 case OPERA:
158 throw new UnsupportedOperationException("Opera is not a supported Browser Type");
159
160 default:
161 log.error("Unknown browser: {}, defaulting to firefox.", BROWSER);
162 browserType = Browser.FIREFOX;
163 driver = FirefoxBrowser.getFirefoxDriver();
164 }
165
166 BrowserUtil.setCurrentBrowser(browserType);
167
168 return new DefaultAtlassianWebDriver(driver, browserType);
169 }
170
171 private static HtmlUnitDriver newHtmlUnitDriver(boolean javascriptEnabled)
172 {
173 final HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24)
174 {
175 @Override
176 protected WebClient modifyWebClient(final WebClient client)
177 {
178
179 client.getOptions().setThrowExceptionOnScriptError(false);
180 return client;
181 }
182 };
183 driver.setJavascriptEnabled(javascriptEnabled);
184 return driver;
185 }
186 }