19
19
20
20
import static com .google .common .base .Preconditions .checkArgument ;
21
21
import static com .google .common .base .Preconditions .checkNotNull ;
22
+ import static org .openqa .selenium .remote .BrowserType .OPERA_BLINK ;
23
+ import static org .openqa .selenium .remote .CapabilityType .BROWSER_NAME ;
22
24
23
- import com .google .common .base .Objects ;
24
25
import com .google .common .collect .ImmutableList ;
25
26
import com .google .common .collect .Lists ;
26
27
import com .google .common .collect .Maps ;
27
28
import com .google .common .io .Files ;
28
- import com .google .gson .Gson ;
29
- import com .google .gson .JsonElement ;
30
29
31
- import org .openqa .selenium .remote .DesiredCapabilities ;
30
+ import org .openqa .selenium .MutableCapabilities ;
31
+ import org .openqa .selenium .WebDriverException ;
32
32
33
33
import java .io .File ;
34
34
import java .io .IOException ;
35
35
import java .util .Base64 ;
36
+ import java .util .Collections ;
36
37
import java .util .List ;
37
38
import java .util .Map ;
39
+ import java .util .TreeMap ;
38
40
39
41
/**
40
42
* Class to manage options specific to {@link OperaDriver}.
48
50
* // For use with OperaDriver:
49
51
* OperaDriver driver = new OperaDriver(options);
50
52
*
51
- * // or alternatively:
52
- * DesiredCapabilities capabilities = DesiredCapabilities.opera();
53
- * capabilities.setCapability(OperaOptions.CAPABILITY, options);
54
- * OperaDriver driver = new OperaDriver(capabilities);
55
- *
56
53
* // For use with RemoteWebDriver:
57
- * DesiredCapabilities capabilities = DesiredCapabilities.opera();
58
- * capabilities.setCapability(ChromeOptions.CAPABILITY, options);
54
+ * OperaOptions options = new OperaOptions();
59
55
* RemoteWebDriver driver = new RemoteWebDriver(
60
- * new URL("http://localhost:4444/wd/hub"), capabilities );
56
+ * new URL("http://localhost:4444/wd/hub"), options );
61
57
* </code></pre>
62
58
*/
63
- public class OperaOptions {
59
+ public class OperaOptions extends MutableCapabilities {
64
60
65
61
/**
66
- * Key used to store a set of OperaOptions in a {@link DesiredCapabilities }
62
+ * Key used to store a set of OperaOptions in a {@link org.openqa.selenium.Capabilities }
67
63
* object.
68
64
*/
69
65
public static final String CAPABILITY = "operaOptions" ;
@@ -74,6 +70,10 @@ public class OperaOptions {
74
70
private List <String > extensions = Lists .newArrayList ();
75
71
private Map <String , Object > experimentalOptions = Maps .newHashMap ();
76
72
73
+ public OperaOptions () {
74
+ setCapability (BROWSER_NAME , OPERA_BLINK );
75
+ }
76
+
77
77
/**
78
78
* Sets the path to the Opera executable. This path should exist on the
79
79
* machine which will launch Opera. The path should either be absolute or
@@ -190,15 +190,12 @@ public Object getExperimentalOption(String name) {
190
190
return experimentalOptions .get (checkNotNull (name ));
191
191
}
192
192
193
- /**
194
- * Converts this instance to its JSON representation.
195
- *
196
- * @return The JSON representation of these options.
197
- * @throws IOException If an error occurs while reading the
198
- * {@link #addExtensions(java.util.List) extension files} from disk.
199
- */
200
- public JsonElement toJson () throws IOException {
201
- Map <String , Object > options = Maps .newHashMap ();
193
+ @ Override
194
+ public Map <String , ?> asMap () {
195
+ Map <String , Object > toReturn = new TreeMap <>();
196
+ toReturn .putAll (super .asMap ());
197
+
198
+ Map <String , Object > options = new TreeMap <>();
202
199
203
200
for (String key : experimentalOptions .keySet ()) {
204
201
options .put (key , experimentalOptions .get (key ));
@@ -213,44 +210,19 @@ public JsonElement toJson() throws IOException {
213
210
List <String > encoded_extensions = Lists .newArrayListWithExpectedSize (
214
211
extensionFiles .size () + extensions .size ());
215
212
for (File path : extensionFiles ) {
216
- String encoded = Base64 .getEncoder ().encodeToString (Files .toByteArray (path ));
217
- encoded_extensions .add (encoded );
213
+ try {
214
+ String encoded = Base64 .getEncoder ().encodeToString (Files .toByteArray (path ));
215
+
216
+ encoded_extensions .add (encoded );
217
+ } catch (IOException e ) {
218
+ throw new WebDriverException (e );
219
+ }
218
220
}
219
221
encoded_extensions .addAll (extensions );
220
222
options .put ("extensions" , encoded_extensions );
221
223
222
- return new Gson ().toJsonTree (options );
223
- }
224
-
225
- /**
226
- * Returns DesiredCapabilities for Opera with these options included as
227
- * capabilities. This does not copy the options. Further changes will be
228
- * reflected in the returned capabilities.
229
- *
230
- * @return DesiredCapabilities for Opera with these options.
231
- */
232
- DesiredCapabilities toCapabilities () {
233
- DesiredCapabilities capabilities = DesiredCapabilities .operaBlink ();
234
- capabilities .setCapability (CAPABILITY , this );
235
- return capabilities ;
236
- }
237
-
238
- @ Override
239
- public boolean equals (Object other ) {
240
- if (!(other instanceof OperaOptions )) {
241
- return false ;
242
- }
243
- OperaOptions that = (OperaOptions ) other ;
244
- return Objects .equal (this .binary , that .binary )
245
- && Objects .equal (this .args , that .args )
246
- && Objects .equal (this .extensionFiles , that .extensionFiles )
247
- && Objects .equal (this .experimentalOptions , that .experimentalOptions )
248
- && Objects .equal (this .extensions , that .extensions );
249
- }
224
+ toReturn .put (CAPABILITY , options );
250
225
251
- @ Override
252
- public int hashCode () {
253
- return Objects .hashCode (this .binary , this .args , this .extensionFiles , this .experimentalOptions ,
254
- this .extensions );
226
+ return Collections .unmodifiableMap (toReturn );
255
227
}
256
228
}
0 commit comments