Skip to content

Commit 6a1b9f4

Browse files
committed
java: Implementing both integer (legacy) and string (standard) response status
1 parent 8e9a802 commit 6a1b9f4

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ public class ErrorCodes {
116116
.put(XPATH_LOOKUP_ERROR, "invalid selector")
117117
.build();
118118

119+
private static Map<String, Integer> stateToStatus;
120+
static {
121+
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.<String, Integer>builder();
122+
for (Map.Entry<Integer, String> pair : statusToState.entrySet()) {
123+
// Ignore duplicate "invalid selector" codes
124+
if (! pair.getValue().equals("invalid selector") || pair.getKey() == INVALID_SELECTOR_ERROR) {
125+
builder.put(pair.getValue(), pair.getKey());
126+
}
127+
}
128+
stateToStatus = builder.build();
129+
}
130+
119131
/**
120132
* Returns the exception type that corresponds to the given {@code statusCode}. All unrecognized
121133
* status codes will be mapped to {@link WebDriverException WebDriverException.class}.
@@ -236,8 +248,11 @@ public boolean isMappableError(Throwable thrown) {
236248
return statusCode != SUCCESS && statusCode != UNHANDLED_ERROR;
237249
}
238250

239-
@Beta
240-
public String toState(int status) {
251+
public static String toState(int status) {
241252
return statusToState.get(status);
242253
}
254+
255+
public static int toStatus(String state) {
256+
return stateToStatus.get(state);
257+
}
243258
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
106106
return (T) new Command(sessionId, name);
107107
}
108108

109+
if (Response.class.equals(clazz)) {
110+
Response response = new Response();
111+
JsonObject json = new JsonParser().parse((String) text).getAsJsonObject();
112+
113+
if (json.has("state") && ! json.get("state").isJsonNull()) {
114+
String state = json.get("state").getAsString();
115+
response.setState(state);
116+
response.setStatus(ErrorCodes.toStatus(state));
117+
}
118+
if (json.has("status") && ! json.get("status").isJsonNull()) {
119+
JsonElement status = json.get("status");
120+
if (status.getAsJsonPrimitive().isString()) {
121+
String state = status.getAsString();
122+
response.setState(state);
123+
response.setStatus(ErrorCodes.toStatus(state));
124+
} else {
125+
int intStatus = status.getAsInt();
126+
response.setState(ErrorCodes.toState(intStatus));
127+
response.setStatus(intStatus);
128+
}
129+
}
130+
131+
response.setValue(convert(Object.class, json.get("value")));
132+
133+
return (T) response;
134+
}
135+
109136
if (SessionId.class.equals(clazz)) {
110137
// Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id.
111138
JsonElement json = text instanceof String

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,42 @@ public void testDecodingResponseWithNumbersInValueObject() {
380380
assertEquals(46.19140625, value.get("height").doubleValue(), 0.00001);
381381
}
382382

383+
@Test
384+
public void testShouldRecognizeNumericStatus() {
385+
Response response = new JsonToBeanConverter()
386+
.convert(Response.class, "{\"status\":0,\"value\":\"cheese\"}");
387+
388+
assertEquals(0, response.getStatus());
389+
assertEquals(ErrorCodes.toState(0), response.getState());
390+
@SuppressWarnings("unchecked")
391+
String value = (String) response.getValue();
392+
assertEquals("cheese", value);
393+
}
394+
395+
@Test
396+
public void testShouldRecognizeStringStatus() {
397+
Response response = new JsonToBeanConverter()
398+
.convert(Response.class, "{\"status\":\"success\",\"value\":\"cheese\"}");
399+
400+
assertEquals(0, response.getStatus());
401+
assertEquals(ErrorCodes.toState(0), response.getState());
402+
@SuppressWarnings("unchecked")
403+
String value = (String) response.getValue();
404+
assertEquals("cheese", value);
405+
}
406+
407+
@Test
408+
public void testShouldRecognizeStringState() {
409+
Response response = new JsonToBeanConverter()
410+
.convert(Response.class, "{\"state\":\"success\",\"value\":\"cheese\"}");
411+
412+
assertEquals("success", response.getState());
413+
assertEquals(0, response.getStatus());
414+
@SuppressWarnings("unchecked")
415+
String value = (String) response.getValue();
416+
assertEquals("cheese", value);
417+
}
418+
383419
public static class SimpleBean {
384420

385421
private String value;

0 commit comments

Comments
 (0)