Skip to content

Commit b99fa0d

Browse files
committed
Start making the SafariOptions w3c safe
1 parent 0efc0be commit b99fa0d

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

java/client/src/org/openqa/selenium/safari/SafariOptions.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class SafariOptions extends MutableCapabilities {
5656

5757
private interface Option {
5858
String TECHNOLOGY_PREVIEW = "technologyPreview";
59+
String TECH_PREVIEW = "se:safari:techPreview";
5960
}
6061

6162
private Map<String, Object> options = new TreeMap<>();
@@ -120,13 +121,15 @@ public static SafariOptions fromCapabilities(Capabilities capabilities)
120121
*/
121122
public SafariOptions setUseTechnologyPreview(boolean useTechnologyPreview) {
122123
options.put(Option.TECHNOLOGY_PREVIEW, useTechnologyPreview);
123-
setCapability(BROWSER_NAME, useTechnologyPreview ? "Safari Technology Preview" : "safari");
124+
// Use an object here, rather than a boolean to avoid a stack overflow
125+
super.setCapability(Option.TECH_PREVIEW, Boolean.valueOf(useTechnologyPreview));
126+
super.setCapability(BROWSER_NAME, useTechnologyPreview ? "Safari Technology Preview" : "safari");
124127
return this;
125128
}
126129

127130
@Override
128131
public void setCapability(String key, Object value) {
129-
if (Option.TECHNOLOGY_PREVIEW.equals(key)) {
132+
if (Option.TECHNOLOGY_PREVIEW.equals(key) || Option.TECH_PREVIEW.equals(key)) {
130133
setUseTechnologyPreview(Boolean.valueOf(value.toString()));
131134
} else {
132135
super.setCapability(key, value);
@@ -135,7 +138,7 @@ public void setCapability(String key, Object value) {
135138

136139
@Override
137140
public void setCapability(String key, boolean value) {
138-
if (Option.TECHNOLOGY_PREVIEW.equals(key)) {
141+
if (Option.TECHNOLOGY_PREVIEW.equals(key) || Option.TECH_PREVIEW.equals(key)) {
139142
setUseTechnologyPreview(value);
140143
} else {
141144
super.setCapability(key, value);
@@ -150,7 +153,7 @@ public SafariOptions setProxy(Proxy proxy) {
150153
// Getters
151154

152155
public boolean getUseTechnologyPreview() {
153-
return (boolean) options.getOrDefault(Option.TECHNOLOGY_PREVIEW, false);
156+
return is(Option.TECH_PREVIEW)|| options.get(Option.TECHNOLOGY_PREVIEW) == Boolean.TRUE;
154157
}
155158

156159
// (De)serialization of the options
@@ -163,9 +166,9 @@ public boolean getUseTechnologyPreview() {
163166
private static SafariOptions fromJsonMap(Map<?, ?> options) {
164167
SafariOptions safariOptions = new SafariOptions();
165168

166-
Boolean useTechnologyPreview = (Boolean) options.get(Option.TECHNOLOGY_PREVIEW);
167-
if (useTechnologyPreview != null) {
168-
safariOptions.setUseTechnologyPreview(useTechnologyPreview);
169+
Object useTechnologyPreview = options.get(Option.TECHNOLOGY_PREVIEW);
170+
if (useTechnologyPreview instanceof Boolean) {
171+
safariOptions.setUseTechnologyPreview((Boolean) useTechnologyPreview);
169172
}
170173

171174
return safariOptions;

java/client/test/org/openqa/selenium/safari/SafariOptionsTest.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
package org.openqa.selenium.safari;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertTrue;
2223

24+
import com.google.common.collect.ImmutableMap;
25+
2326
import org.junit.Test;
2427
import org.openqa.selenium.ImmutableCapabilities;
2528

29+
import java.util.HashMap;
30+
import java.util.Map;
31+
2632
public class SafariOptionsTest {
2733

2834
@Test
@@ -38,8 +44,31 @@ public void roundTrippingToCapabilitiesAndBackWorks() {
3844

3945
@Test
4046
public void canConstructFromCapabilities() {
41-
SafariOptions options = new SafariOptions(
42-
new ImmutableCapabilities("technologyPreview", true));
47+
Map<String, Object> embeddedOptions = new HashMap<>();
48+
embeddedOptions.put("technologyPreview", true);
49+
50+
SafariOptions options = new SafariOptions();
51+
assertFalse(options.getUseTechnologyPreview());
52+
53+
options = new SafariOptions(new ImmutableCapabilities(SafariOptions.CAPABILITY, embeddedOptions));
54+
assertTrue(options.getUseTechnologyPreview());
55+
56+
embeddedOptions.put("technologyPreview", false);
57+
options = new SafariOptions(new ImmutableCapabilities(SafariOptions.CAPABILITY, embeddedOptions));
58+
assertFalse(options.getUseTechnologyPreview());
59+
60+
options = new SafariOptions(new ImmutableCapabilities("se:safari:techPreview", true));
61+
assertTrue(options.getUseTechnologyPreview());
62+
63+
options = new SafariOptions(new ImmutableCapabilities("se:safari:techPreview", false));
64+
assertFalse(options.getUseTechnologyPreview());
65+
}
66+
67+
@Test
68+
public void newerStyleCapabilityWinsOverOlderStyle() {
69+
SafariOptions options = new SafariOptions(new ImmutableCapabilities(
70+
SafariOptions.CAPABILITY, ImmutableMap.of("technologyPreview", false),
71+
"se:safari:techPreview", true));
4372

4473
assertTrue(options.getUseTechnologyPreview());
4574
}
@@ -56,4 +85,3 @@ public void settingTechnologyPreviewModeAlsoChangesBrowserName() {
5685
assertEquals("safari", options.getBrowserName());
5786
}
5887
}
59-

0 commit comments

Comments
 (0)