Skip to content

Commit

Permalink
Update the w3c parameters for the protocol handshake to be spec compl…
Browse files Browse the repository at this point in the history
…iant

The correct format nests the `alwaysMatch` and `firstMatch` in a
`capabilities` property, rather than being at the root of the
new session blob. Thanks to @jimevans for pointing this out.
  • Loading branch information
shs96c committed Mar 26, 2017
1 parent 7a1df57 commit f8b23bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ public Result createSession(HttpClient client, Command command)
out.beginObject();

streamJsonWireProtocolParameters(out, gson, des, req);

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

out.endObject();
out.flush();
Expand Down Expand Up @@ -382,13 +386,10 @@ private void streamGeckoDriver013Parameters(
Gson gson,
JsonObject des,
JsonObject req) throws IOException {
out.name("capabilities");
out.beginObject();
out.name("desiredCapabilities");
gson.toJson(des, out);
out.name("requiredCapabilities");
gson.toJson(req, out);
out.endObject(); // End "capabilities"
}

public class Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@
import static java.net.HttpURLConnection.HTTP_OK;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.util.Map;

@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
public class ProtocolHandshakeTest {
Expand Down Expand Up @@ -97,9 +100,10 @@ public void requestShouldIncludeSpecCompliantW3CCapabilities() throws IOExceptio
HttpRequest request = client.getRequest();
Map<String, Object> json = new Gson()
.fromJson(request.getContentString(), new TypeToken<Map<String, Object>>(){}.getType());
Map<String, Object> caps = (Map<String, Object>) json.get("capabilities");

assertEquals(ImmutableMap.of(), json.get("alwaysMatch"));
assertEquals(ImmutableList.of(), json.get("firstMatch"));
assertEquals(ImmutableMap.of(), caps.get("alwaysMatch"));
assertEquals(ImmutableList.of(), caps.get("firstMatch"));
}

@Test
Expand Down Expand Up @@ -150,6 +154,39 @@ public void shouldParseWireProtocolNewSessionResponse() throws IOException {
assertEquals(result.getDialect(), Dialect.OSS);
}

@Test
public void shouldAddBothGeckoDriverAndW3CCapabilitiesToRootCapabilitiesProperty()
throws IOException {
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
Command command = new Command(null, DriverCommand.NEW_SESSION, params);

HttpResponse response = new HttpResponse();
response.setStatus(HTTP_OK);
response.setContent(
"{\"sessionId\": \"23456789\", \"status\": 0, \"value\": {}}".getBytes(UTF_8));
RecordingHttpClient client = new RecordingHttpClient(response);

new ProtocolHandshake().createSession(client, command);

HttpRequest request = client.getRequest();
Map<String, Object> handshakeRequest = new Gson().fromJson(
request.getContentString(),
new TypeToken<Map<String, Object>>() {}.getType());

Object rawCaps = handshakeRequest.get("capabilities");
assertTrue(rawCaps instanceof Map);

Map<?, ?> capabilities = (Map<?, ?>) rawCaps;

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

// W3C
assertTrue(capabilities.containsKey("alwaysMatch"));
assertTrue(capabilities.containsKey("firstMatch"));
}

class RecordingHttpClient implements HttpClient {

private final HttpResponse response;
Expand Down

0 comments on commit f8b23bb

Please sign in to comment.