21
21
import com .google .gson .JsonObject ;
22
22
23
23
import org .openqa .selenium .Capabilities ;
24
+ import org .openqa .selenium .MutableCapabilities ;
25
+ import org .openqa .selenium .Platform ;
24
26
import org .openqa .selenium .WebDriverException ;
27
+ import org .openqa .selenium .remote .CapabilityType ;
25
28
import org .openqa .selenium .remote .DesiredCapabilities ;
26
29
27
30
import java .io .IOException ;
28
31
import java .util .Map ;
32
+ import java .util .TreeMap ;
29
33
30
34
/**
31
35
* Class to manage options specific to {@link SafariDriver}.
39
43
* SafariDriver driver = new SafariDriver(options);
40
44
*
41
45
* // For use with RemoteWebDriver:
42
- * DesiredCapabilities capabilities = DesiredCapabilities.safari();
43
- * capabilities.setCapability(SafariOptions.CAPABILITY, options);
44
46
* RemoteWebDriver driver = new RemoteWebDriver(
45
- * new URL("http://localhost:4444/wd/hub"), capabilities);
47
+ * new URL("http://localhost:4444/wd/hub"),
48
+ * options);
46
49
* </code></pre>
47
50
*/
48
- public class SafariOptions {
51
+ public class SafariOptions extends MutableCapabilities {
49
52
50
53
/**
51
54
* Key used to store SafariOptions in a {@link DesiredCapabilities} object.
@@ -60,20 +63,16 @@ private Option() {} // Utility class.
60
63
private static final String PORT = "port" ;
61
64
}
62
65
63
- /**
64
- * @see #setPort(int)
65
- */
66
- private int port = 0 ;
66
+ private Map <String , Object > options = new TreeMap <>();
67
67
68
- /**
69
- * @see #setUseCleanSession(boolean)
70
- */
71
- private boolean useCleanSession = false ;
68
+ public SafariOptions () {
69
+ setCapability ( Option . PORT , 0 );
70
+ setCapability ( Option . CLEAN_SESSION , false );
71
+ setCapability ( Option . TECHNOLOGY_PREVIEW , false ) ;
72
72
73
- /**
74
- * @see #setUseTechnologyPreview(boolean)
75
- */
76
- private boolean useTechnologyPreview = false ;
73
+ setCapability (CapabilityType .BROWSER_NAME , "safari" );
74
+ setCapability (CapabilityType .PLATFORM , Platform .MAC );
75
+ }
77
76
78
77
/**
79
78
* Construct a {@link SafariOptions} instance from given capabilites.
@@ -109,7 +108,7 @@ public static SafariOptions fromCapabilities(Capabilities capabilities)
109
108
* or 0 if the server should select a free port.
110
109
*/
111
110
SafariOptions setPort (int port ) {
112
- this . port = port ;
111
+ options . put ( Option . PORT , port ) ;
113
112
return this ;
114
113
}
115
114
@@ -124,7 +123,7 @@ SafariOptions setPort(int port) {
124
123
* @param useCleanSession If true, the SafariDriver will erase all existing session data.
125
124
*/
126
125
public SafariOptions setUseCleanSession (boolean useCleanSession ) {
127
- this . useCleanSession = useCleanSession ;
126
+ options . put ( Option . CLEAN_SESSION , useCleanSession ) ;
128
127
return this ;
129
128
}
130
129
@@ -136,7 +135,7 @@ public SafariOptions setUseCleanSession(boolean useCleanSession) {
136
135
* otherwise will use the release version of Safari.
137
136
*/
138
137
public SafariOptions setUseTechnologyPreview (boolean useTechnologyPreview ) {
139
- this . useTechnologyPreview = useTechnologyPreview ;
138
+ options . put ( Option . TECHNOLOGY_PREVIEW , useTechnologyPreview ) ;
140
139
return this ;
141
140
}
142
141
@@ -148,45 +147,27 @@ public SafariOptions setUseTechnologyPreview(boolean useTechnologyPreview) {
148
147
* @see #setPort(int)
149
148
*/
150
149
public int getPort () {
151
- return port ;
150
+ return ( int ) options . getOrDefault ( Option . PORT , 0 ) ;
152
151
}
153
152
154
153
/**
155
154
* @return Whether the SafariDriver should erase all session data before launching Safari.
156
155
* @see #setUseCleanSession(boolean)
157
156
*/
158
157
public boolean getUseCleanSession () {
159
- return useCleanSession ;
158
+ return ( boolean ) options . getOrDefault ( Option . CLEAN_SESSION , false ) ;
160
159
}
161
160
162
161
public boolean getUseTechnologyPreview () {
163
- return useTechnologyPreview ;
162
+ return ( boolean ) options . getOrDefault ( Option . TECHNOLOGY_PREVIEW , false ) ;
164
163
}
165
164
166
165
// (De)serialization of the options
167
166
168
- /**
169
- * Converts this instance to its JSON representation.
170
- *
171
- * @return The JSON representation of the options.
172
- * @throws IOException If an error occurred while reading the Safari extension files.
173
- */
174
- public JsonObject toJson () throws IOException {
175
- JsonObject options = new JsonObject ();
176
- options .addProperty (Option .PORT , port );
177
- options .addProperty (Option .CLEAN_SESSION , useCleanSession );
178
- options .addProperty (Option .TECHNOLOGY_PREVIEW , useTechnologyPreview );
179
- return options ;
180
- }
181
-
182
167
/**
183
168
* Parse a Map and reconstruct the {@link SafariOptions}.
184
- * A temporary directory is created to hold all Safari extension files.
185
169
*
186
- * @param options A Map derived from the output of {@link #toJson()}.
187
170
* @return A {@link SafariOptions} instance associated with these extensions.
188
- * @throws IOException If an error occurred while writing the safari extensions to a
189
- * temporary directory.
190
171
*/
191
172
private static SafariOptions fromJsonMap (Map <?, ?> options ) throws IOException {
192
173
SafariOptions safariOptions = new SafariOptions ();
@@ -215,26 +196,10 @@ private static SafariOptions fromJsonMap(Map<?, ?> options) throws IOException {
215
196
* reflected in the returned capabilities.
216
197
*
217
198
* @return DesiredCapabilities for Safari with these extensions.
199
+ * @deprecated {@code SafariOptions} are already {@link MutableCapabilities}.
218
200
*/
219
- DesiredCapabilities toCapabilities () {
220
- DesiredCapabilities capabilities = DesiredCapabilities .safari ();
221
- capabilities .setCapability (CAPABILITY , this );
222
- return capabilities ;
223
- }
224
-
225
- @ Override
226
- public boolean equals (Object other ) {
227
- if (!(other instanceof SafariOptions )) {
228
- return false ;
229
- }
230
- SafariOptions that = (SafariOptions ) other ;
231
- return this .port == that .port
232
- && this .useCleanSession == that .useCleanSession
233
- && this .useTechnologyPreview == that .useTechnologyPreview ;
234
- }
235
-
236
- @ Override
237
- public int hashCode () {
238
- return Objects .hashCode (this .port , this .useCleanSession , this .useTechnologyPreview );
201
+ @ Deprecated
202
+ MutableCapabilities toCapabilities () {
203
+ return this ;
239
204
}
240
205
}
0 commit comments