1
1
package com .saucelabs .remotedriver ;
2
2
3
+ import com .saucelabs .common .SauceApi ;
4
+ import com .saucelabs .saucerest .SauceREST ;
3
5
import org .openqa .selenium .MutableCapabilities ;
4
6
import org .openqa .selenium .WebDriver ;
5
7
import org .openqa .selenium .chrome .ChromeOptions ;
6
8
import org .openqa .selenium .edge .EdgeOptions ;
7
9
import org .openqa .selenium .firefox .FirefoxOptions ;
8
10
import org .openqa .selenium .ie .InternetExplorerOptions ;
9
11
import org .openqa .selenium .remote .CapabilityType ;
12
+ import org .openqa .selenium .remote .RemoteWebDriver ;
10
13
import org .openqa .selenium .safari .SafariOptions ;
11
14
12
15
import java .net .MalformedURLException ;
@@ -17,15 +20,17 @@ public class SauceSession {
17
20
static String SAUCE_USERNAME = System .getenv ("SAUCE_USERNAME" );
18
21
static String SAUCE_ACCESS_KEY = System .getenv ("SAUCE_ACCESS_KEY" );
19
22
20
- //todo there is some weird bug when this is set to Linux, the session can't be started
23
+ String BUILD_TAG = System .getenv ("BUILD_TAG" );
24
+
25
+ public SauceApi test ;
26
+
27
+ //todo there is some weird bug when this is set to Linux, the session can't be started
21
28
String operatingSystem = "Windows 10" ;
22
29
String browserName = "Chrome" ;
23
30
String testName ;
24
31
Boolean useSauce = true ;
25
32
String sauceOptionsTag = "sauce:options" ;
26
33
27
- public String seleniumServer ;
28
-
29
34
ChromeOptions chromeOptions ;
30
35
FirefoxOptions firefoxOptions ;
31
36
MutableCapabilities sauceOptions ;
@@ -38,7 +43,10 @@ public class SauceSession {
38
43
private EdgeOptions edgeOptions ;
39
44
private InternetExplorerOptions ieOptions ;
40
45
41
- public SauceSession (){
46
+ private String sessionId ;
47
+ private SauceREST api ;
48
+
49
+ public SauceSession () {
42
50
capabilities = new MutableCapabilities ();
43
51
remoteDriverManager = new ConcreteRemoteDriverManager ();
44
52
}
@@ -50,18 +58,14 @@ public SauceSession(RemoteDriverInterface remoteManager) {
50
58
51
59
public SauceSession start () throws MalformedURLException
52
60
{
53
- seleniumServer = getSeleniumServer ();
54
61
capabilities = getCapabilities ();
55
- webDriver = remoteDriverManager .getRemoteWebDriver (seleniumServer , capabilities );
56
- return this ;
62
+ webDriver = remoteDriverManager .getRemoteWebDriver (sauceSeleniumServer , capabilities );
63
+ sessionId = ((RemoteWebDriver ) webDriver ).getSessionId ().toString ();
64
+ test = new SauceApi (webDriver );
65
+ api = new SauceREST (SAUCE_USERNAME , SAUCE_ACCESS_KEY );
66
+
67
+ return this ;
57
68
}
58
- public String getSeleniumServer () {
59
- if (seleniumServer == null )
60
- {
61
- seleniumServer = sauceSeleniumServer ;
62
- }
63
- return seleniumServer ;
64
- }
65
69
public MutableCapabilities getCapabilities () {
66
70
sauceOptions = getSauceOptions ();
67
71
setBrowserOptions (browserName );
@@ -78,17 +82,19 @@ public MutableCapabilities getSauceOptions()
78
82
{
79
83
if (useSauce )
80
84
{
81
- seleniumServer = sauceSeleniumServer ;
82
-
83
85
sauceOptions = new MutableCapabilities ();
84
86
sauceOptions .setCapability ("username" , SAUCE_USERNAME );
85
87
sauceOptions .setCapability ("accessKey" , SAUCE_ACCESS_KEY );
86
- sauceOptions .setCapability ("seleniumVersion" , "3.141.59" );
87
88
88
89
if (testName != null )
89
90
{
90
91
sauceOptions .setCapability ("name" , testName );
91
92
}
93
+
94
+ if (BUILD_TAG != null )
95
+ {
96
+ sauceOptions .setCapability ("build" , BUILD_TAG );
97
+ }
92
98
}
93
99
94
100
return sauceOptions ;
@@ -98,27 +104,23 @@ public void setBrowserOptions(String browserName)
98
104
{
99
105
if (browserName .equalsIgnoreCase ("Chrome" ))
100
106
{
101
- withChrome ();
102
107
capabilities .setCapability (ChromeOptions .CAPABILITY , chromeOptions );
103
108
}
104
109
else if (browserName .equalsIgnoreCase ("Firefox" ))
105
110
{
106
- withFirefox ();
107
111
capabilities .setCapability (FirefoxOptions .FIREFOX_OPTIONS , firefoxOptions );
108
112
}
109
113
else if (browserName .equalsIgnoreCase ("Safari" ))
110
114
{
111
- withSafari ();
115
+ safariOptions = new SafariOptions ();
112
116
capabilities .setCapability (SafariOptions .CAPABILITY , safariOptions );
113
117
}
114
118
else if (browserName .equalsIgnoreCase ("Edge" ))
115
119
{
116
- withEdge ();
117
120
capabilities .setCapability ("Edge" , edgeOptions );
118
121
}
119
122
else if (browserName .equalsIgnoreCase ("IE" ))
120
123
{
121
- withIE ();
122
124
capabilities .setCapability ("se:ieOptions" , ieOptions );
123
125
}
124
126
else {
@@ -159,13 +161,18 @@ public WebDriver getDriver() {
159
161
}
160
162
161
163
//TODO How do we want to handle this?
162
- //1. withSafari (OperatingSystem.MacOs1014), aka, force the user to pass in a mac version
163
- //2. throw an exception for withSafari () used without withMac();
164
- //3. this is the method I chose below: withSafari (String browserVersion)
165
- public SauceSession withSafari () {
164
+ //1. withMacOsMojave (OperatingSystem.MacOs1014), aka, force the user to pass in a mac version
165
+ //2. throw an exception for withMacOsMojave () used without withMac();
166
+ //3. this is the method I chose below: withMacOsMojave (String browserVersion)
167
+ public SauceSession withMacOsMojave () {
166
168
operatingSystem = "macOS 10.14" ;
167
169
browserName = "Safari" ;
168
- safariOptions = new SafariOptions ();
170
+ return this ;
171
+ }
172
+ public SauceSession withMacOsHighSierra ()
173
+ {
174
+ this .operatingSystem = "macOS 10.13" ;
175
+ browserName = "Safari" ;
169
176
return this ;
170
177
}
171
178
public SauceSession withBrowserVersion (String browserVersion ){
@@ -189,4 +196,71 @@ public SauceSession withPlatform(String operatingSystem) {
189
196
this .operatingSystem = operatingSystem ;
190
197
return this ;
191
198
}
199
+
200
+
201
+ public SauceSession withBrowser (String browserName , String browserVersion )
202
+ {
203
+ this .browserName = browserName ;
204
+ this .browserVersion = browserVersion ;
205
+ setBrowserOptions (browserName );
206
+
207
+ return this ;
208
+ }
209
+
210
+ public SauceSession withBrowser (String browserName )
211
+ {
212
+ this .browserName = browserName ;
213
+ setBrowserOptions (browserName );
214
+
215
+ return this ;
216
+ }
217
+
218
+ public SauceSession withTestName (String testName )
219
+ {
220
+ this .testName = testName ;
221
+ return this ;
222
+ }
223
+
224
+ public SauceSession withBuildName (String buildName )
225
+ {
226
+ this .testName = testName ;
227
+ return this ;
228
+ }
229
+
230
+ public void stop ()
231
+ {
232
+ if (webDriver != null )
233
+ webDriver .quit ();
234
+ }
235
+
236
+ public void passed ()
237
+ {
238
+ if (webDriver != null ) { test .setTestStatus ("passed" ); }
239
+ else {
240
+ api .jobPassed (sessionId );
241
+ }
242
+ }
243
+
244
+ public void failed ()
245
+ {
246
+ if (webDriver != null ) { test .setTestStatus ("failed" ); }
247
+ else {
248
+ api .jobFailed (sessionId );
249
+ }
250
+ }
251
+
252
+ public void setTestName (String testName )
253
+ {
254
+ this .testName = testName ;
255
+ }
256
+
257
+ public void setBuildTag (String buildTag )
258
+ {
259
+ this .BUILD_TAG = buildTag ;
260
+ }
261
+
262
+ public void setBuild (String build )
263
+ {
264
+ test .setBuildName (build );
265
+ }
192
266
}
0 commit comments