Java Code Examples for org.openqa.selenium.remote.Response#setSessionId()

The following examples show how to use org.openqa.selenium.remote.Response#setSessionId() . 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: JsonTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void canHandleValueBeingAnArray() {
  String[] value = {"Cheese", "Peas"};

  Response response = new Response();
  response.setSessionId("bar");
  response.setValue(value);
  response.setStatus(1512);

  String json = new Json().toJson(response);
  Response converted = new Json().toType(json, Response.class);

  assertThat(response.getSessionId()).isEqualTo("bar");
  assertThat(((List<?>) converted.getValue())).hasSize(2);
  assertThat(response.getStatus().intValue()).isEqualTo(1512);
}
 
Example 2
Source File: Responses.java    From selenium with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a response object for a failed command execution.
 *
 * @param sessionId ID of the session that executed the command.
 * @param reason the failure reason.
 * @param screenshot a base64 png screenshot to include with the failure.
 * @return the new response object.
 */
public static Response failure(
    SessionId sessionId, Throwable reason, Optional<String> screenshot) {
  Response response = new Response();
  response.setSessionId(sessionId != null ? sessionId.toString() : null);
  response.setStatus(ERROR_CODES.toStatusCode(reason));
  response.setState(ERROR_CODES.toState(response.getStatus()));

  if (reason != null) {
    Json json = new Json();
    String raw = json.toJson(reason);
    Map<String, Object> value = json.toType(raw, MAP_TYPE);
    screenshot.ifPresent(screen -> value.put("screen", screen));
    response.setValue(value);
  }
  return response;
}
 
Example 3
Source File: Responses.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a response object for a successful command execution.
 *
 * @param sessionId ID of the session that executed the command.
 * @param value the command result value.
 * @return the new response object.
 */
public static Response success(SessionId sessionId, Object value) {
  Response response = new Response();
  response.setSessionId(sessionId != null ? sessionId.toString() : null);
  response.setValue(value);
  response.setStatus(ErrorCodes.SUCCESS);
  response.setState(ErrorCodes.SUCCESS_STRING);
  return response;
}
 
Example 4
Source File: Responses.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a response object for a failed command execution.
 *
 * @param sessionId ID of the session that executed the command.
 * @param reason the failure reason.
 * @return the new response object.
 */
public static Response failure(SessionId sessionId, Throwable reason) {
  Response response = new Response();
  response.setSessionId(sessionId != null ? sessionId.toString() : null);
  response.setValue(reason);
  response.setStatus(ERROR_CODES.toStatusCode(reason));
  response.setState(ERROR_CODES.toState(response.getStatus()));
  return response;
}
 
Example 5
Source File: CustomHttpCommandExecutor.java    From hifive-pitalium with Apache License 2.0 4 votes vote down vote up
public Response execute(Command command) throws IOException {
	if (command.getSessionId() == null) {
		if (QUIT.equals(command.getName())) {
			return new Response();
		}
		if (!GET_ALL_SESSIONS.equals(command.getName()) && !NEW_SESSION.equals(command.getName())) {
			throw new NoSuchSessionException("Session ID is null. Using WebDriver after calling quit()?");
		}
	}

	if (NEW_SESSION.equals(command.getName())) {
		if (commandCodec != null) {
			throw new SessionNotCreatedException("Session already exists");
		}
		ProtocolHandshake handshake = new ProtocolHandshake();
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
		ProtocolHandshake.Result result = handshake.createSession(client, command);
		dialect = result.getDialect();
		commandCodec = dialect.getCommandCodec();
		for (Map.Entry<String, CommandInfo> entry : additionalCommands.entrySet()) {
			defineCommand(entry.getKey(), entry.getValue());
		}
		responseCodec = dialect.getResponseCodec();
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));
		return result.createResponse();
	}

	if (commandCodec == null || responseCodec == null) {
		throw new WebDriverException("No command or response codec has been defined. Unable to proceed");
	}

	HttpRequest httpRequest = commandCodec.encode(command);
	try {
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
		HttpResponse httpResponse = client.execute(httpRequest, true);
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));

		Response response = responseCodec.decode(httpResponse);
		if (response.getSessionId() == null) {
			if (httpResponse.getTargetHost() != null) {
				response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost()));
			} else {
				// Spam in the session id from the request
				response.setSessionId(command.getSessionId().toString());
			}
		}
		if (QUIT.equals(command.getName())) {
			client.close();
		}
		return response;
	} catch (UnsupportedCommandException e) {
		if (e.getMessage() == null || "".equals(e.getMessage())) {
			throw new UnsupportedOperationException(
					"No information from server. Command name was: " + command.getName(), e.getCause());
		}
		throw e;
	}
}