diff --git a/java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java b/java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java index 019e8a98008d4..b5ec4172d99b8 100644 --- a/java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java +++ b/java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java @@ -314,11 +314,32 @@ private Optional createSession(HttpClient client, InputStream newSession jsonBlob = new HashMap<>(); } - // If the result looks positive, return the result. - Object sessionId = jsonBlob.get("sessionId"); Object value = jsonBlob.get("value"); Object w3cError = jsonBlob.get("error"); Object ossStatus = jsonBlob.get("status"); + + // If the result was an error that we believe has to do with the remote end failing to start the + // session, create an exception and throw it. + Response tempResponse = null; + if (response.getStatus() != HttpURLConnection.HTTP_OK) { + tempResponse = new Response(null); + tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED); + tempResponse.setValue(value); + } else if ("session not created".equals(w3cError)) { + tempResponse = new Response(null); + tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED); + tempResponse.setValue(value); + } else if (ossStatus instanceof Number && ((Number) ossStatus).intValue() != 0) { + tempResponse = new Response(null); + tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED); + tempResponse.setValue(value); + } + + if (tempResponse != null) { + new ErrorHandler().throwIfResponseFailed(tempResponse, 0); + } + + Object sessionId = jsonBlob.get("sessionId"); Map capabilities = null; // The old geckodriver prior to 0.14 returned "value" as the thing containing the session id. @@ -342,29 +363,10 @@ private Optional createSession(HttpClient client, InputStream newSession capabilities = ((Capabilities) capabilities).asMap(); } - if (response.getStatus() == HttpURLConnection.HTTP_OK) { - if (sessionId != null && capabilities != null) { - Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS; - return Optional.of( - new Result(dialect, String.valueOf(sessionId), capabilities)); - } - } - - // If the result was an error that we believe has to do with the remote end failing to start the - // session, create an exception and throw it. - Response tempResponse = null; - if ("session not created".equals(w3cError)) { - tempResponse = new Response(null); - tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED); - tempResponse.setValue(jsonBlob); - } else if (ossStatus instanceof Number) { - tempResponse = new Response(null); - tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED); - tempResponse.setValue(jsonBlob); - } - - if (tempResponse != null) { - new ErrorHandler().throwIfResponseFailed(tempResponse, 0); + // If the result looks positive, return the result. + if (sessionId != null && capabilities != null) { + Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS; + return Optional.of(new Result(dialect, String.valueOf(sessionId), capabilities)); } // Otherwise, just return empty.