...27import org.openqa.selenium.WebDriverException;28import org.openqa.selenium.json.Json;29import org.openqa.selenium.remote.ErrorCodes;30import org.openqa.selenium.remote.Response;31import org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec;32import org.openqa.selenium.remote.http.HttpResponse;33import java.io.Serializable;34import java.util.HashMap;35import java.util.Map;36public class W3CHttpResponseCodecTest {37 @Test38 public void noErrorNoCry() {39 Map<String, Object> data = new HashMap<>();40 data.put("value", "cheese");41 HttpResponse response = createValidResponse(HTTP_OK, data);42 Response decoded = new W3CHttpResponseCodec().decode(response);43 assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.SUCCESS);44 assertThat(decoded.getState()).isEqualTo("success");45 assertThat(decoded.getValue()).isEqualTo("cheese");46 }47 @Test48 public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementations() {49 Map<String, Object> error = new HashMap<>();50 error.put("error", "unsupported operation"); // 50051 error.put("message", "I like peas");52 error.put("stacktrace", "");53 HttpResponse response = createValidResponse(HTTP_INTERNAL_ERROR, error);54 Response decoded = new W3CHttpResponseCodec().decode(response);55 assertThat(decoded.getState()).isEqualTo("unsupported operation");56 assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED);57 assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class);58 assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas");59 }60 @Test61 public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForConformingImplementations() {62 Map<String, Object> error = new HashMap<>();63 error.put("error", "unsupported operation"); // 50064 error.put("message", "I like peas");65 error.put("stacktrace", "");66 Map<String, Object> data = new HashMap<>();67 data.put("value", error);68 HttpResponse response = createValidResponse(HTTP_INTERNAL_ERROR, data);69 Response decoded = new W3CHttpResponseCodec().decode(response);70 assertThat(decoded.getState()).isEqualTo("unsupported operation");71 assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED);72 assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class);73 assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas");74 }75 @Test76 public void shouldPopulateTheAlertTextIfThrowingAnUnhandledAlertException() {77 ImmutableMap<String, ImmutableMap<String, Serializable>> data = ImmutableMap.of(78 "value", ImmutableMap.of(79 "error", "unexpected alert open",80 "message", "Modal dialog present",81 "stacktrace", "",82 "data", ImmutableMap.of("text", "cheese")));83 HttpResponse response = createValidResponse(500, data);84 Response decoded = new W3CHttpResponseCodec().decode(response);85 UnhandledAlertException ex = (UnhandledAlertException) decoded.getValue();86 assertThat(ex.getAlertText()).isEqualTo("cheese");87 }88 private HttpResponse createValidResponse(int statusCode, Map<String, ?> data) {89 byte[] contents = new Json().toJson(data).getBytes(UTF_8);90 HttpResponse response = new HttpResponse();91 response.setStatus(statusCode);92 response.addHeader("Content-Type", "application/json; charset=utf-8");93 response.addHeader("Cache-Control", "no-cache");94 response.addHeader("Content-Length", String.valueOf(contents.length));95 response.setContent(contents);96 return response;97 }98}...