1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.samplu.common;
17
18 import com.saucelabs.common.SauceOnDemandAuthentication;
19 import com.saucelabs.common.SauceOnDemandSessionIdProvider;
20 import com.saucelabs.junit.SauceOnDemandTestWatcher;
21 import com.saucelabs.saucerest.SauceREST;
22 import org.junit.Assert;
23 import org.openqa.selenium.Platform;
24 import org.openqa.selenium.WebDriver;
25 import org.openqa.selenium.ie.InternetExplorerDriver;
26 import org.openqa.selenium.remote.DesiredCapabilities;
27 import org.openqa.selenium.remote.RemoteWebDriver;
28
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.net.URL;
34 import java.util.HashMap;
35 import java.util.Map;
36
37
38
39
40
41
42
43
44
45 public class SauceLabsWebDriverHelper implements SauceOnDemandSessionIdProvider {
46
47
48
49
50
51 public static final String REMOTE_DRIVER_SAUCELABS_PROPERTY = "remote.driver.saucelabs";
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static final String SAUCE_BROWSER_PROPERTY = "saucelabs.browser";
66
67
68
69
70
71 public static final String SAUCE_BUILD_PROPERTY = "rice.version";
72
73
74
75
76
77
78
79 public static final String SAUCE_DOWNLOAD_SCRIPT_PROPERTY = "saucelabs.download.scripts";
80
81
82
83
84
85 public static final String SAUCE_IDLE_TIMEOUT_SECONDS_PROPERTY = "saucelabs.idle.timeout.seconds";
86
87
88
89
90
91 public static final String SAUCE_KEY_PROPERTY = "saucelabs.key";
92
93
94
95
96
97 public static final String SAUCE_MAX_DURATION_SECONDS_PROPERTY = "saucelabs.max.duration.seconds";
98
99
100
101
102
103 public static final String SAUCE_PLATFORM_PROPERTY = "saucelabs.platform";
104
105
106
107
108
109 public static final String SAUCE_IE_INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS_PROPERTY = "saucelabs.ie.ignore.domains";
110
111
112
113
114
115 public static final String SAUCE_POPUP_PROPERTY = "saucelabs.pop.disable";
116
117
118
119
120
121 public static final String SAUCE_SHARE_PROPERTY = "saucelabs.share";
122
123
124
125
126
127 public static final String SAUCE_USER_PROPERTY = "saucelabs.user";
128
129
130
131
132
133
134 public static final String SAUCE_VERSION_PROPERTY = "saucelabs.browser.version";
135
136
137
138
139
140 public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(System.getProperty(SAUCE_USER_PROPERTY), System.getProperty(SAUCE_KEY_PROPERTY));
141
142 private WebDriver driver;
143
144 private String sessionId;
145
146
147
148
149
150
151
152 public void setUp(String className, String testName) throws Exception {
153 if (System.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY) == null) {
154 return;
155 }
156
157 if (System.getProperty(SAUCE_USER_PROPERTY) == null || System.getProperty(SAUCE_KEY_PROPERTY) == null) {
158 Assert.fail("-D" + SAUCE_USER_PROPERTY + " and -D" + SAUCE_KEY_PROPERTY + " must be set to saucelabs user and access key.");
159 }
160
161 DesiredCapabilities capabilities = null;
162 if ("ff".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
163 capabilities = DesiredCapabilities.firefox();
164 } else if ("ie".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
165 capabilities = DesiredCapabilities.internetExplorer();
166 capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
167 System.getProperty(SAUCE_IE_INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS_PROPERTY, "true"));
168 } else if ("chrome".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
169 capabilities = DesiredCapabilities.chrome();
170 } else if ("opera".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
171 capabilities = DesiredCapabilities.opera();
172 } else if ("android".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
173 capabilities = DesiredCapabilities.android();
174 } else if ("safari".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
175 capabilities = DesiredCapabilities.safari();
176 } else if ("ipad".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
177 capabilities = DesiredCapabilities.ipad();
178 } else if ("iphone".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
179 capabilities = DesiredCapabilities.iphone();
180 } else {
181 capabilities = DesiredCapabilities.firefox();
182 }
183
184 String version = System.getProperty(SAUCE_VERSION_PROPERTY);
185 if (version == null || "0".equals(version)) {
186
187 if (!"chrome".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
188 throw new RuntimeException("Blank or 0 version for a browser not chrome " + System.getProperty(SAUCE_BROWSER_PROPERTY));
189 }
190
191 capabilities.setCapability("version", "");
192 } else {
193 capabilities.setCapability("version", version);
194 }
195
196 capabilities.setCapability("platform", System.getProperty(SAUCE_PLATFORM_PROPERTY, Platform.UNIX.toString()).replaceAll("_", " "));
197 capabilities.setCapability("idle-timeout", Integer.parseInt(System.getProperty(SAUCE_IDLE_TIMEOUT_SECONDS_PROPERTY, "180")));
198 capabilities.setCapability("max-duration", Integer.parseInt(System.getProperty(SAUCE_MAX_DURATION_SECONDS_PROPERTY, "480")));
199 capabilities.setCapability("name", className + "." + testName + "-" + ITUtil.DTS);
200 capabilities.setCapability("disable-popup-handler", System.getProperty(SAUCE_POPUP_PROPERTY, "false"));
201 capabilities.setCapability("public", System.getProperty(SAUCE_SHARE_PROPERTY, "share"));
202
203 this.driver = new RemoteWebDriver(
204 new URL("http://" + authentication.getUsername() + ":" + authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"),
205 capabilities);
206 this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString();
207
208
209 if ("true".equals(System.getProperty(SAUCE_DOWNLOAD_SCRIPT_PROPERTY, "false"))) {
210 try {
211 String dir = determineSaveDir(className, testName);
212 String resources = "mkdir " + dir + " ; cd " + dir + " ; \n"
213 + curlSaveResourceString(className, testName, "selenium-server.log") + " ; \n"
214 + curlSaveResourceString(className, testName, "video.flv") + " ; \n"
215
216 + "cd ../\n";
217 System.out.println(resources);
218 writeFile("SauceLabsResources" + dir + ".sh", resources);
219 } catch (Exception e) {
220 System.out.println("Exception while writing SauceLabsResources.sh " + e.getMessage());
221 System.out.println(curlSaveResourceString(className, testName, "selenium-server.log"));
222 System.out.println(curlSaveResourceString(className, testName, "video.flv"));
223
224 }
225 }
226 }
227
228
229
230
231
232
233
234
235
236 public static void tearDown(boolean passed, String sessionId, String sauceUser, String sauceKey) throws Exception {
237 if (sessionId != null && System.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY) != null) {
238 SauceREST client = new SauceREST(sauceUser, sauceKey);
239
240
241
242 Map<String, Object> updates = new HashMap<String, Object>();
243 updates.put("passed", passed);
244 updates.put("build", System.getProperty(SAUCE_BUILD_PROPERTY, "unknown"));
245 client.updateJobInfo(sessionId, updates);
246
247 if (passed) {
248 System.out.println("Registering session passed " + sessionId);
249 client.jobPassed(sessionId);
250 } else {
251 System.out.println("Registering session failed " + sessionId);
252 client.jobFailed(sessionId);
253 }
254
255 Thread.sleep(5000);
256 }
257 }
258
259 private String curlSaveResourceString(String className, String testName, String resource) {
260 return "curl -o " + deriveResourceBaseNames(className, testName, resource)
261 + " -u " + authentication.getUsername() + ":" + authentication.getAccessKey()
262 + " http://saucelabs.com/rest/" + authentication.getUsername()+ "/jobs/" + sessionId + "/results/" + resource;
263 }
264
265 private String deriveResourceBaseNames(String className, String testName, String resource) {
266 return className + "." + testName + "-"
267 + System.getProperty(SAUCE_PLATFORM_PROPERTY, Platform.UNIX.toString()) + "-"
268 + System.getProperty(SAUCE_BROWSER_PROPERTY) + "-"
269 + System.getProperty(SAUCE_VERSION_PROPERTY) + "-"
270 + System.getProperty(WebDriverLegacyITBase.REMOTE_PUBLIC_USER_PROPERTY, "admin") + "-"
271 + System.getProperty(SAUCE_BUILD_PROPERTY, "unknown_build") + "-"
272 + ITUtil.DTS + "-"
273 + resource;
274 }
275
276
277
278
279
280 public WebDriver getDriver() {
281 return driver;
282 }
283
284 @Override
285 public String getSessionId() {
286 return sessionId;
287 }
288
289
290 private String wgetnSaveResourceString(String className, String testName) {
291 String dir = determineSaveDir(className, testName);
292
293 return "wgetn https://saucelabs.com/rest/" + authentication.getUsername()+ "/jobs/"
294 + sessionId + "/results/%04dscreenshot.jpg 0 50";
295 }
296
297 private String determineSaveDir(String className, String testName) {
298 String dir = deriveResourceBaseNames(className, testName, "");
299 dir = dir.substring(0, dir.length() -1);
300 return dir;
301 }
302
303 private void writeFile(String fileName, String content) throws IOException {
304 File file = new File(fileName);
305
306 if (!file.exists()) {
307 file.createNewFile();
308 }
309
310 FileWriter fw = new FileWriter(file.getAbsoluteFile());
311 BufferedWriter bw = new BufferedWriter(fw);
312 bw.write(content);
313 bw.flush();
314 bw.close();
315 }
316 }