Skip to content

Commit eb7d9bf

Browse files
committed
Fix tests failing because of ProtocolHandshake
1 parent 2cc2ecf commit eb7d9bf

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

java/client/src/org/openqa/selenium/remote/NewSessionPayload.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ public void writeTo(Appendable appendable) throws IOException {
217217
try (JsonOutput json = new Json().newOutput(appendable)) {
218218
json.beginObject();
219219

220-
@SuppressWarnings("unchecked")
221-
Map<String, Object> first = (Map<String, Object>) stream().findFirst()
222-
.orElse(new ImmutableCapabilities())
223-
.asMap();
220+
Map<String, Object> first = getOss();
221+
if (first == null) {
222+
//noinspection unchecked
223+
first = (Map<String, Object>) stream().findFirst()
224+
.orElse(new ImmutableCapabilities())
225+
.asMap();
226+
}
224227

225228
// Write the first capability we get as the desired capability.
226229
json.name("desiredCapabilities");
@@ -295,7 +298,7 @@ public Stream<ImmutableCapabilities> stream() throws IOException {
295298
}
296299

297300
public ImmutableSet<Dialect> getDownstreamDialects() {
298-
return dialects.isEmpty() ? ImmutableSet.of(Dialect.OSS) : dialects;
301+
return dialects.isEmpty() ? ImmutableSet.of(DEFAULT_DIALECT) : dialects;
299302
}
300303

301304
@Override
@@ -325,7 +328,7 @@ private Stream<Map<String, Object>> getW3C() throws IOException {
325328
// then add magic to generate each of the w3c capabilities. For the sake of simplicity, we're
326329
// going to make the (probably wrong) assumption we can hold all of the firstMatch values and
327330
// alwaysMatch value in memory at the same time.
328-
Map<String, Object> oss = getOss();
331+
Map<String, Object> oss = convertOssToW3C(getOss());
329332
Stream<Map<String, Object>> fromOss;
330333
if (oss != null) {
331334
Set<String> usedKeys = new HashSet<>();
@@ -357,11 +360,10 @@ private Stream<Map<String, Object>> getW3C() throws IOException {
357360
// into the stream to form a unified set of capabilities. Woohoo!
358361
fromOss = firsts.stream()
359362
.map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build())
360-
.map(this::convertOssToW3C)
363+
.map(this::applyTransforms)
361364
.map(map -> map.entrySet().stream()
362365
.filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))
363366
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)))
364-
.filter(map -> !map.isEmpty())
365367
.map(map -> (Map<String, Object>) map);
366368
} else {
367369
fromOss = Stream.of();
@@ -386,10 +388,14 @@ private Stream<Map<String, Object>> getW3C() throws IOException {
386388
.map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build());
387389
}
388390

389-
return Stream.concat(fromOss, fromW3c);
391+
return Stream.concat(fromOss, fromW3c).distinct();
390392
}
391393

392-
private Map<String, Object> convertOssToW3C(ImmutableMap<String, Object> capabilities) {
394+
private Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {
395+
if (capabilities == null) {
396+
return null;
397+
}
398+
393399
Map<String, Object> toReturn = new TreeMap<>();
394400
toReturn.putAll(capabilities);
395401

java/client/src/org/openqa/selenium/remote/session/ChromeFilter.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,36 @@
1818
package org.openqa.selenium.remote.session;
1919

2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.ImmutableSortedMap;
22+
import com.google.common.collect.Ordering;
2123

2224
import java.util.Map;
2325
import java.util.Objects;
26+
import java.util.TreeMap;
27+
import java.util.function.Supplier;
28+
import java.util.stream.Collectors;
2429

2530
public class ChromeFilter implements CapabilitiesFilter {
2631
@Override
2732
public Map<String, Object> apply(Map<String, Object> unmodifiedCaps) {
28-
ImmutableMap<String, Object> caps = unmodifiedCaps.entrySet().parallelStream()
33+
Map<String, Object> caps = unmodifiedCaps.entrySet().parallelStream()
2934
.filter(
3035
entry ->
3136
("browserName".equals(entry.getKey()) && "chrome".equals(entry.getValue())) ||
3237
entry.getKey().startsWith("goog:") ||
3338
"chromeOptions".equals(entry.getKey()))
3439
.filter(entry -> Objects.nonNull(entry.getValue()))
3540
.distinct()
36-
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
41+
.collect(Collectors.toMap(
42+
Map.Entry::getKey,
43+
Map.Entry::getValue,
44+
(l, r) -> r,
45+
TreeMap::new));
46+
47+
// We may need to map the chromeoptions to the new form
48+
if (caps.containsKey("chromeOptions") && !caps.containsKey("goog:chromeOptions")) {
49+
caps.put("goog:chromeOptions", caps.get("chromeOptions"));
50+
}
3751

3852
return caps.isEmpty() ? null : caps;
3953
}

java/client/src/org/openqa/selenium/remote/session/FirefoxFilter.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,33 @@
1717

1818
package org.openqa.selenium.remote.session;
1919

20-
import com.google.common.collect.ImmutableMap;
21-
2220
import java.util.Map;
2321
import java.util.Objects;
22+
import java.util.TreeMap;
23+
import java.util.stream.Collectors;
2424

2525
public class FirefoxFilter implements CapabilitiesFilter {
2626
// Note: we don't take a dependency on the FirefoxDriver jar as it might not be on the classpath
2727

2828
@Override
2929
public Map<String, Object> apply(Map<String, Object> unmodifiedCaps) {
30-
ImmutableMap<String, Object> caps = unmodifiedCaps.entrySet().parallelStream()
30+
Map<String, Object> caps = unmodifiedCaps.entrySet().parallelStream()
3131
.filter(entry ->
3232
("browserName".equals(entry.getKey()) && "firefox".equals(entry.getValue())) ||
3333
entry.getKey().startsWith("firefox_") ||
3434
entry.getKey().startsWith("moz:"))
3535
.filter(entry -> Objects.nonNull(entry.getValue()))
36-
.distinct()
37-
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
36+
.collect(Collectors.toMap(
37+
Map.Entry::getKey,
38+
Map.Entry::getValue,
39+
(l, r) -> l,
40+
TreeMap::new));
41+
42+
// If we only have marionette in the caps, the user is asking for firefox. Make sure we inject
43+
// the browser name to be sure.
44+
if (unmodifiedCaps.containsKey("marionette") && !caps.containsKey("browserName")) {
45+
caps.put("browserName", "firefox");
46+
}
3847

3948
return caps.isEmpty() ? null : caps;
4049
}

java/client/test/org/openqa/selenium/remote/ProtocolHandshakeTest.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ public void shouldNotIncludeNonProtocolExtensionKeys() throws IOException {
217217

218218
HttpRequest request = client.getRequest();
219219

220-
System.out.println(request.getContentString());
221-
222220
Map<String, Object> handshakeRequest = new Gson().fromJson(
223221
request.getContentString(),
224222
new TypeToken<Map<String, Object>>() {}.getType());
@@ -267,20 +265,15 @@ public void firstMatchSeparatesCapsForDifferentBrowsers() throws IOException {
267265

268266
List<Map<String, Object>> capabilities = mergeW3C(handshakeRequest);
269267

270-
System.out.println("capabilities = " + capabilities);
271-
272268
assertThat(capabilities, containsInAnyOrder(
273-
// OSS with valid w3c keys. It's fine that this won't match
274-
ImmutableMap.of("browserName", "chrome", "moz:firefoxOptions", ImmutableMap.of()),
275-
// Generated capabilities
276269
ImmutableMap.of("moz:firefoxOptions", ImmutableMap.of()),
277270
ImmutableMap.of("browserName", "chrome")));
278271
}
279272

280273
@Test
281274
public void doesNotCreateFirstMatchForNonW3CCaps() throws IOException {
282275
Capabilities caps = new ImmutableCapabilities(
283-
"chromeOptions", ImmutableMap.of(),
276+
"cheese", ImmutableMap.of(),
284277
"moz:firefoxOptions", ImmutableMap.of(),
285278
"browserName", "firefox");
286279

@@ -337,8 +330,6 @@ public void shouldLowerCaseProxyTypeForW3CRequest() throws IOException {
337330
assertEquals("autodetect", seenProxy.get("proxyType"));
338331
});
339332

340-
Object rawCaps = handshakeRequest.get("capabilities");
341-
342333
Map<String, ?> jsonCaps = (Map<String, ?>) handshakeRequest.get("desiredCapabilities");
343334
Map<String, ?> seenProxy = (Map<String, ?>) jsonCaps.get("proxy");
344335
assertEquals("AUTODETECT", seenProxy.get("proxyType"));

java/server/test/org/openqa/selenium/remote/server/SyntheticNewSessionPayloadTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ public void ossPayloadWillBeFirstW3CPayload() {
157157

158158
List<Capabilities> allCaps = getCapabilities(rawCapabilities);
159159

160-
assertEquals(2, allCaps.size());
160+
assertEquals(3, allCaps.size());
161161
assertEquals(false, allCaps.get(0).getCapability("marionette"));
162162
}
163163

164164
private List<Capabilities> getCapabilities(Map<String, Object> payload) {
165165
try (NewSessionPayload newSessionPayload = NewSessionPayload.create(payload)) {
166+
StringBuilder b = new StringBuilder();
167+
newSessionPayload.writeTo(b);
168+
System.out.println("b = " + b);
166169
return newSessionPayload.stream().collect(ImmutableList.toImmutableList());
167170
} catch (IOException e) {
168171
throw new UncheckedIOException(e);

0 commit comments

Comments
 (0)