Skip to content

Commit 6ee36e8

Browse files
committed
Handle a Selenium 3 style "sessionId" in a Command received by a Selenium 2 server.
Note, this doesn't include the opposite conversion in the BeanToJsonConverter, since every server out there is still a normal v2 one.
1 parent 10cc303 commit 6ee36e8

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ private <T> T convert(Class<T> clazz, Object text, int depth) throws JSONExcepti
9898
}
9999

100100
if (SessionId.class.equals(clazz)) {
101-
JSONObject object = new JSONObject((String) text);
102-
String value = object.getString("value");
103-
return (T) new SessionId(value);
101+
// Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id.
102+
String coerced = String.valueOf(text);
103+
if (!coerced.isEmpty() && coerced.charAt(0) == '{') {
104+
JSONObject object = new JSONObject(coerced);
105+
String value = object.getString("value");
106+
return (T) new SessionId(value);
107+
}
108+
109+
return (T) new SessionId(coerced);
104110
}
105111

106112
if (Capabilities.class.equals(clazz)) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import static org.junit.Assert.assertTrue;
4949
import static org.junit.Assert.fail;
5050

51+
import com.google.common.collect.Maps;
52+
5153
@RunWith(JUnit4.class)
5254
public class JsonToBeanConverterTest {
5355

@@ -338,6 +340,23 @@ public void testShouldNotParseQuotedJsonObjectsAsActualJsonObjects() throws JSON
338340
assertThat(convertedInner.toString(), equalTo(inner.toString()));
339341
}
340342

343+
@Test
344+
public void shouldBeAbleToConvertASelenium3CommandToASelenium2Command() throws JSONException {
345+
SessionId expectedId = new SessionId("thisisakey");
346+
347+
JSONObject rawJson = new JSONObject();
348+
// In selenium 2, the sessionId is an object. In selenium 3, it's a straight string.
349+
rawJson.put("sessionId", expectedId.toString());
350+
rawJson.put("name", "some command");
351+
rawJson.put("parameters", Maps.newHashMap());
352+
353+
String stringified = rawJson.toString();
354+
355+
Command converted = new JsonToBeanConverter().convert(Command.class, stringified);
356+
357+
assertEquals(expectedId, converted.getSessionId());
358+
}
359+
341360
public static class SimpleBean {
342361

343362
private String value;

0 commit comments

Comments
 (0)