Skip to content

Commit

Permalink
Remove requiredCapabilities from ProtocolHandshake
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 3, 2017
1 parent 456d9f0 commit 788936f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
55 changes: 15 additions & 40 deletions java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;

import org.openqa.selenium.Capabilities;
Expand All @@ -54,7 +53,6 @@
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
Expand Down Expand Up @@ -96,12 +94,9 @@ public Result createSession(HttpClient client, Command command)
throws IOException {
Capabilities desired = (Capabilities) command.getParameters().get("desiredCapabilities");
desired = desired == null ? new ImmutableCapabilities() : desired;
Capabilities required = (Capabilities) command.getParameters().get("requiredCapabilities");
required = required == null ? new ImmutableCapabilities() : required;

BeanToJsonConverter converter = new BeanToJsonConverter();
JsonObject des = (JsonObject) converter.convertObject(desired);
JsonObject req = (JsonObject) converter.convertObject(required);

// We don't know how large the generated JSON is going to be. Spool it to disk, and then read
// the file size, then stream it to the remote end. If we could be sure the remote end could
Expand All @@ -116,12 +111,12 @@ public Result createSession(HttpClient client, Command command)
Gson gson = new Gson();
out.beginObject();

streamJsonWireProtocolParameters(out, gson, des, req);
streamJsonWireProtocolParameters(out, gson, des);

out.name("capabilities");
out.beginObject();
streamGeckoDriver013Parameters(out, gson, des, req);
streamW3CProtocolParameters(out, gson, des, req);
streamGeckoDriver013Parameters(out, gson, des);
streamW3CProtocolParameters(out, gson, des);
out.endObject();

out.endObject();
Expand All @@ -146,27 +141,22 @@ public Result createSession(HttpClient client, Command command)
throw new SessionNotCreatedException(
String.format(
"Unable to create new remote session. " +
"desired capabilities = %s, required capabilities = %s",
desired,
required));
"desired capabilities = %s",
desired));
}

private void streamJsonWireProtocolParameters(
JsonWriter out,
Gson gson,
JsonObject des,
JsonObject req) throws IOException {
JsonObject des) throws IOException {
out.name("desiredCapabilities");
gson.toJson(des, out);
out.name("requiredCapabilities");
gson.toJson(req, out);
}

private void streamW3CProtocolParameters(
JsonWriter out,
Gson gson,
JsonObject des,
JsonObject req) throws IOException {
JsonObject des) throws IOException {
// Technically we should be building up a combination of "alwaysMatch" and "firstMatch" options.
// We're going to do a little processing to figure out what we might be able to do, and assume
// that people don't really understand the difference between required and desired (which is
Expand All @@ -187,35 +177,27 @@ private void streamW3CProtocolParameters(
// We can't use the constants defined in the classes because it would introduce circular
// dependencies between the remote library and the implementations. Yay!

Map<String, ?> chrome = Stream.of(des, req)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> chrome = des.entrySet().stream()
.filter(entry ->
("browserName".equals(entry.getKey()) && CHROME.equals(entry.getValue().getAsString())) ||
"chromeOptions".equals(entry.getKey()))
.distinct()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));

Map<String, ?> edge = Stream.of(des, req)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> edge = des.entrySet().stream()
.filter(entry -> ("browserName".equals(entry.getKey()) && EDGE.equals(entry.getValue().getAsString())))
.distinct()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));

Map<String, ?> firefox = Stream.of(des, req)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> firefox = des.entrySet().stream()
.filter(entry ->
("browserName".equals(entry.getKey()) && FIREFOX.equals(entry.getValue().getAsString())) ||
entry.getKey().startsWith("firefox_") ||
entry.getKey().startsWith("moz:"))
.distinct()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));

Map<String, ?> ie = Stream.of(req, des)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> ie = des.entrySet().stream()
.filter(entry ->
("browserName".equals(entry.getKey()) && IE.equals(entry.getValue().getAsString())) ||
"browserAttachTimeout".equals(entry.getKey()) ||
Expand All @@ -234,19 +216,15 @@ private void streamW3CProtocolParameters(
.distinct()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));

Map<String, ?> opera = Stream.of(des, req)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> opera = des.entrySet().stream()
.filter(entry ->
("browserName".equals(entry.getKey()) && OPERA_BLINK.equals(entry.getValue().getAsString())) ||
("browserName".equals(entry.getKey()) && OPERA.equals(entry.getValue().getAsString())) ||
"operaOptions".equals(entry.getKey()))
.distinct()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));

Map<String, ?> safari = Stream.of(des, req)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
Map<String, ?> safari = des.entrySet().stream()
.filter(entry ->
("browserName".equals(entry.getKey()) && SAFARI.equals(entry.getValue().getAsString())) ||
"safari.options".equals(entry.getKey()))
Expand All @@ -259,7 +237,7 @@ private void streamW3CProtocolParameters(
.distinct()
.collect(ImmutableSet.toImmutableSet());

JsonObject alwaysMatch = Stream.of(des, req)
JsonObject alwaysMatch = Stream.of(des)
.map(JsonObject::entrySet)
.flatMap(Collection::stream)
.filter(entry -> !excludedKeys.contains(entry.getKey()))
Expand Down Expand Up @@ -363,12 +341,9 @@ public Optional<Result> createSession(HttpClient client, InputStream newSessionB
private void streamGeckoDriver013Parameters(
JsonWriter out,
Gson gson,
JsonObject des,
JsonObject req) throws IOException {
JsonObject des) throws IOException {
out.name("desiredCapabilities");
gson.toJson(des, out);
out.name("requiredCapabilities");
gson.toJson(req, out);
}

public static class Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void requestShouldIncludeJsonWireProtocolCapabilities() throws IOExceptio
.fromJson(request.getContentString(), new TypeToken<Map<String, Object>>(){}.getType());

assertEquals(ImmutableMap.of(), json.get("desiredCapabilities"));
assertEquals(ImmutableMap.of(), json.get("requiredCapabilities"));
}

@Test
Expand All @@ -98,7 +97,6 @@ public void requestShouldIncludeOlderGeckoDriverCapabilities() throws IOExceptio
Map<String, Object> capabilities = (Map<String, Object>) json.get("capabilities");

assertEquals(ImmutableMap.of(), capabilities.get("desiredCapabilities"));
assertEquals(ImmutableMap.of(), capabilities.get("requiredCapabilities"));
}

@Test
Expand Down Expand Up @@ -197,7 +195,6 @@ public void shouldAddBothGeckoDriverAndW3CCapabilitiesToRootCapabilitiesProperty

// GeckoDriver
assertTrue(capabilities.containsKey("desiredCapabilities"));
assertTrue(capabilities.containsKey("requiredCapabilities"));

// W3C
assertTrue(capabilities.containsKey("alwaysMatch"));
Expand Down

0 comments on commit 788936f

Please sign in to comment.