Skip to content

Commit c19bba0

Browse files
committed
Correctly parse the session ID in W3C spec-compliant new session responses.
They look like: { "value": { "sessionId": string, "capabilities": Object } }
1 parent c9fe229 commit c19bba0

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,14 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession
348348
// Pull that out if it exists.
349349
if (value != null && value instanceof Map) {
350350
Map<?, ?> mappedValue = (Map<?, ?>) value;
351-
if (mappedValue.containsKey("value") && mappedValue.containsKey("sessionId")) {
352-
value = mappedValue.get("value");
351+
if (mappedValue.containsKey("sessionId")) {
353352
sessionId = mappedValue.get("sessionId");
354353
}
354+
if (mappedValue.containsKey("capabilities")) {
355+
value = mappedValue.get("capabilities");
356+
} else if (mappedValue.containsKey("value")) {
357+
value = mappedValue.get("value");
358+
}
355359
if (mappedValue.containsKey("error")) {
356360
w3cError = mappedValue.get("error");
357361
}

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

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@
2525
import com.google.common.collect.ImmutableMap;
2626
import com.google.gson.Gson;
2727
import com.google.gson.reflect.TypeToken;
28-
28+
import java.io.IOException;
29+
import java.util.Map;
2930
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
3033
import org.openqa.selenium.remote.http.HttpClient;
3134
import org.openqa.selenium.remote.http.HttpRequest;
3235
import org.openqa.selenium.remote.http.HttpResponse;
3336

34-
import java.io.IOException;
35-
import java.util.Map;
36-
3737
@SuppressWarnings("unchecked")
38+
@RunWith(JUnit4.class)
3839
public class ProtocolHandshakeTest {
3940

4041
@Test
41-
public void shouldIncludeJsonWireProtocolCapabilities() throws IOException {
42+
public void requestShouldIncludeJsonWireProtocolCapabilities() throws IOException {
4243
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
4344
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
4445

4546
HttpResponse response = new HttpResponse();
4647
response.setStatus(HTTP_OK);
47-
response.setContent("{\"value\": {\"sessionId\": \"23456789\", \"value\": {}}}".getBytes(UTF_8));
48+
response.setContent(
49+
"{\"value\": {\"sessionId\": \"23456789\", \"capabilities\": {}}}".getBytes(UTF_8));
4850
RecordingHttpClient client = new RecordingHttpClient(response);
4951

5052
new ProtocolHandshake().createSession(client, command);
@@ -58,13 +60,14 @@ public void shouldIncludeJsonWireProtocolCapabilities() throws IOException {
5860
}
5961

6062
@Test
61-
public void shouldIncludeOlderGeckoDriverCapabilities() throws IOException {
63+
public void requestShouldIncludeOlderGeckoDriverCapabilities() throws IOException {
6264
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
6365
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
6466

6567
HttpResponse response = new HttpResponse();
6668
response.setStatus(HTTP_OK);
67-
response.setContent("{\"value\": {\"sessionId\": \"23456789\", \"value\": {}}}".getBytes(UTF_8));
69+
response.setContent(
70+
"{\"value\": {\"sessionId\": \"23456789\", \"capabilities\": {}}}".getBytes(UTF_8));
6871
RecordingHttpClient client = new RecordingHttpClient(response);
6972

7073
new ProtocolHandshake().createSession(client, command);
@@ -79,13 +82,14 @@ public void shouldIncludeOlderGeckoDriverCapabilities() throws IOException {
7982
}
8083

8184
@Test
82-
public void shouldIncludeSpecCompliantW3CCapabilities() throws IOException {
85+
public void requestShouldIncludeSpecCompliantW3CCapabilities() throws IOException {
8386
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
8487
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
8588

8689
HttpResponse response = new HttpResponse();
8790
response.setStatus(HTTP_OK);
88-
response.setContent("{\"value\": {\"sessionId\": \"23456789\", \"value\": {}}}".getBytes(UTF_8));
91+
response.setContent(
92+
"{\"value\": {\"sessionId\": \"23456789\", \"capabilities\": {}}}".getBytes(UTF_8));
8993
RecordingHttpClient client = new RecordingHttpClient(response);
9094

9195
new ProtocolHandshake().createSession(client, command);
@@ -98,6 +102,54 @@ public void shouldIncludeSpecCompliantW3CCapabilities() throws IOException {
98102
assertEquals(ImmutableList.of(), json.get("firstMatch"));
99103
}
100104

105+
@Test
106+
public void shouldParseW3CNewSessionResponse() throws IOException {
107+
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
108+
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
109+
110+
HttpResponse response = new HttpResponse();
111+
response.setStatus(HTTP_OK);
112+
response.setContent(
113+
"{\"value\": {\"sessionId\": \"23456789\", \"capabilities\": {}}}".getBytes(UTF_8));
114+
RecordingHttpClient client = new RecordingHttpClient(response);
115+
116+
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);
117+
assertEquals(result.getDialect(), Dialect.W3C);
118+
}
119+
120+
@Test
121+
public void shouldParseOlderW3CNewSessionResponse() throws IOException {
122+
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
123+
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
124+
125+
HttpResponse response = new HttpResponse();
126+
response.setStatus(HTTP_OK);
127+
// Some drivers (e.g., GeckoDriver 0.15.0) return the capabilities in a key named "value",
128+
// rather than "capabilities"; essentially this is the old Wire Protocol format, wrapped in a
129+
// "value" key.
130+
response.setContent(
131+
"{\"value\": {\"sessionId\": \"23456789\", \"value\": {}}}".getBytes(UTF_8));
132+
RecordingHttpClient client = new RecordingHttpClient(response);
133+
134+
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);
135+
assertEquals(result.getDialect(), Dialect.W3C);
136+
}
137+
138+
@Test
139+
public void shouldParseWireProtocolNewSessionResponse() throws IOException {
140+
Map<String, Object> params = ImmutableMap.of("desiredCapabilities", new DesiredCapabilities());
141+
Command command = new Command(null, DriverCommand.NEW_SESSION, params);
142+
143+
HttpResponse response = new HttpResponse();
144+
response.setStatus(HTTP_OK);
145+
response.setContent(
146+
"{\"sessionId\": \"23456789\", \"status\": 0, \"value\": {}}".getBytes(UTF_8));
147+
RecordingHttpClient client = new RecordingHttpClient(response);
148+
149+
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);
150+
assertEquals(result.getDialect(), Dialect.OSS);
151+
}
152+
101153
class RecordingHttpClient implements HttpClient {
102154

103155
private final HttpResponse response;
@@ -123,4 +175,4 @@ public HttpRequest getRequest() {
123175
return request;
124176
}
125177
}
126-
}
178+
}

0 commit comments

Comments
 (0)