Java Code Examples for org.openqa.selenium.remote.DriverCommand#GET

The following examples show how to use org.openqa.selenium.remote.DriverCommand#GET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtocolConverterTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldRoundTripASimpleCommand() throws IOException {
  SessionId sessionId = new SessionId("1234567");

  HttpHandler handler = new ProtocolConverter(
      tracer,
      HttpClient.Factory.createDefault().createClient(new URL("http://example.com/wd/hub")),
      W3C,
      OSS) {
    @Override
    protected HttpResponse makeRequest(HttpRequest request) {
      HttpResponse response = new HttpResponse();

      response.setHeader("Content-Type", MediaType.JSON_UTF_8.toString());
      response.setHeader("Cache-Control", "none");

      Map<String, Object> obj = new HashMap<>();
      obj.put("sessionId", sessionId.toString());
      obj.put("status", 0);
      obj.put("value", null);
      String payload = json.toJson(obj);
      response.setContent(utf8String(payload));

      return response;
    }
  };

  Command command = new Command(
      sessionId,
      DriverCommand.GET,
      ImmutableMap.of("url", "http://example.com/cheese"));

  HttpRequest w3cRequest = new W3CHttpCommandCodec().encode(command);

  HttpResponse resp = handler.execute(w3cRequest);

  assertEquals(MediaType.JSON_UTF_8, MediaType.parse(resp.getHeader("Content-type")));
  assertEquals(HttpURLConnection.HTTP_OK, resp.getStatus());

  Map<String, Object> parsed = json.toType(string(resp), MAP_TYPE);
  assertNull(parsed.toString(), parsed.get("sessionId"));
  assertTrue(parsed.toString(), parsed.containsKey("value"));
  assertNull(parsed.toString(), parsed.get("value"));
}
 
Example 2
Source File: ProtocolConverterTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldConvertAnException() throws IOException {
  // Json upstream, w3c downstream
  SessionId sessionId = new SessionId("1234567");

  HttpHandler handler = new ProtocolConverter(
      tracer,
      HttpClient.Factory.createDefault().createClient(new URL("http://example.com/wd/hub")),
      W3C,
      OSS) {
    @Override
    protected HttpResponse makeRequest(HttpRequest request) {
      HttpResponse response = new HttpResponse();

      response.setHeader("Content-Type", MediaType.JSON_UTF_8.toString());
      response.setHeader("Cache-Control", "none");

     String payload = new Json().toJson(
         ImmutableMap.of(
             "sessionId", sessionId.toString(),
             "status", UNHANDLED_ERROR,
             "value", new WebDriverException("I love cheese and peas")));
      response.setContent(utf8String(payload));
      response.setStatus(HTTP_INTERNAL_ERROR);

      return response;
    }
  };

  Command command = new Command(
      sessionId,
      DriverCommand.GET,
      ImmutableMap.of("url", "http://example.com/cheese"));

  HttpRequest w3cRequest = new W3CHttpCommandCodec().encode(command);

  HttpResponse resp = handler.execute(w3cRequest);

  assertEquals(MediaType.JSON_UTF_8, MediaType.parse(resp.getHeader("Content-type")));
  assertEquals(HTTP_INTERNAL_ERROR, resp.getStatus());

  Map<String, Object> parsed = json.toType(string(resp), MAP_TYPE);
  assertNull(parsed.get("sessionId"));
  assertTrue(parsed.containsKey("value"));
  @SuppressWarnings("unchecked") Map<String, Object> value =
      (Map<String, Object>) parsed.get("value");
  System.out.println("value = " + value.keySet());
  assertEquals("unknown error", value.get("error"));
  assertTrue(((String) value.get("message")).startsWith("I love cheese and peas"));
}