14
14
import java .net .URL ;
15
15
16
16
public class SauceSession {
17
+ @ Getter protected RemoteWebDriver driver ;
17
18
@ Getter @ Setter private DataCenter dataCenter = DataCenter .US_WEST ;
18
19
@ Getter private final SauceOptions sauceOptions ;
19
20
@ Setter private URL sauceUrl ;
20
21
@ Getter private String result ;
21
22
22
- @ Getter protected RemoteWebDriver driver ;
23
-
24
23
public SauceSession () {
25
24
this (new SauceOptions ());
26
25
}
27
26
28
27
/**
29
- * Ideally the end user calls build() on Configurations instance
30
- * this constructor is being accommodating in case they do not
28
+ * Ideally the end user calls build() on Configurations instance.
29
+ * This constructor is being accommodating in case they do not.
31
30
*
32
31
* @param configs the instance of Configuration used to create the Options
33
32
*/
@@ -39,11 +38,19 @@ public SauceSession(SauceOptions options) {
39
38
sauceOptions = options ;
40
39
}
41
40
41
+ /**
42
+ * Starts the session on Sauce Labs.
43
+ *
44
+ * @return the driver instance for using as normal in your tests.
45
+ */
42
46
public RemoteWebDriver start () {
43
47
this .driver = createRemoteWebDriver (getSauceUrl (), sauceOptions .toCapabilities ());
44
48
return driver ;
45
49
}
46
50
51
+ /**
52
+ * @return the full URL for sending to Sauce Labs based on the desired data center.
53
+ */
47
54
public URL getSauceUrl () {
48
55
try {
49
56
if (sauceUrl != null ) {
@@ -56,14 +63,26 @@ public URL getSauceUrl() {
56
63
}
57
64
}
58
65
59
- protected RemoteWebDriver createRemoteWebDriver (URL url , Capabilities capabilities ) {
60
- return new RemoteWebDriver (url , capabilities );
61
- }
62
-
66
+ /**
67
+ * Analyzes Accessibility for the current page.
68
+ * User can work with the results from the Results object or see them
69
+ * in the accessibility tab in the Sauce Labs UI.
70
+ *
71
+ * @return an object with the accessibility analysis
72
+ */
63
73
public Results getAccessibilityResults () {
64
74
return getAccessibilityResults (true );
65
75
}
66
76
77
+ /**
78
+ * Analyzes Accessibility for the current page.
79
+ * User can work with the results from the Results object or see them
80
+ * in the accessibility tab in the Sauce Labs UI.
81
+ *
82
+ * @param frames whether the page being evaluated needs to dig into frames.
83
+ * passing false here will slightly improve performance.
84
+ * @return an object with the accessibility analysis
85
+ */
67
86
public Results getAccessibilityResults (boolean frames ) {
68
87
AxeBuilder axeBuilder = new AxeBuilder ();
69
88
if (!frames ) {
@@ -72,33 +91,66 @@ public Results getAccessibilityResults(boolean frames) {
72
91
return getAccessibilityResults (axeBuilder );
73
92
}
74
93
94
+ /**
95
+ * Analyzes Accessibility for the current page.
96
+ * User can work with the results from the Results object or see them
97
+ * in the accessibility tab in the Sauce Labs UI.
98
+ *
99
+ * @param builder for advanced accessibility information provide your own
100
+ * instance of an AxeBuilder.
101
+ * @return an object with the accessibility analysis
102
+ */
75
103
public Results getAccessibilityResults (AxeBuilder builder ) {
76
104
return builder .analyze (driver );
77
105
}
78
106
107
+ /**
108
+ * Ends the session on Sauce Labs and quits the driver.
109
+ * It requires reporting whether the test has passed or failed.
110
+ *
111
+ * @param passed true if the test has passed, otherwise false
112
+ */
79
113
public void stop (Boolean passed ) {
80
- String result = passed ? "passed" : "failed" ;
81
- stop (result );
114
+ if (this .driver != null ) {
115
+ String update = passed ? "passed" : "failed" ;
116
+ updateResult (update );
117
+ }
82
118
}
83
119
120
+ /**
121
+ * @deprecated Do not use magic strings, pass in boolean for whether test has passed.
122
+ */
123
+ @ Deprecated
84
124
public void stop (String result ) {
85
125
if (this .driver != null ) {
86
126
updateResult (result );
87
127
quit ();
88
128
}
89
129
}
90
130
131
+ /**
132
+ * Not intended for subclassing.
133
+ * Package-private for testing.
134
+ *
135
+ * @param url to send session commands to
136
+ * @param capabilities configuration for session
137
+ * @return driver instance
138
+ */
139
+ RemoteWebDriver createRemoteWebDriver (URL url , Capabilities capabilities ) {
140
+ return new RemoteWebDriver (url , capabilities );
141
+ }
142
+
91
143
private void updateResult (String result ) {
92
144
this .result = result ;
93
145
getDriver ().executeScript ("sauce:job-result=" + result );
94
146
95
147
// Add output for the Sauce OnDemand Jenkins plugin
96
148
// The first print statement will automatically populate links on Jenkins to Sauce
97
149
// The second print statement will output the job link to logging/console
98
- String sauceReporter = String .format ("SauceOnDemandSessionID=%s job-name=%s" ,
150
+ final String sauceReporter = String .format ("SauceOnDemandSessionID=%s job-name=%s" ,
99
151
this .driver .getSessionId (),
100
152
this .sauceOptions .sauce ().getName ());
101
- String sauceTestLink = String .format ("Test Job Link: https://app.saucelabs.com/tests/%s" ,
153
+ final String sauceTestLink = String .format ("Test Job Link: https://app.saucelabs.com/tests/%s" ,
102
154
this .driver .getSessionId ());
103
155
System .out .print (sauceReporter + "\n " + sauceTestLink + "\n " );
104
156
}
0 commit comments