private String sendMessage(Class<? extends WebDriverException> throwOnFailure, String methodName, Object... parameters) { return sendMessage(throwOnFailure, new Command(context, methodName, parameters)); }
private String convert(Command command) { JSONObject json = new JSONObject(); try { json.put("commandName", command.getCommandName()); json.put("context", String.valueOf(command.getContext())); json.put("elementId", command.getElementId()); JSONArray params = new JSONArray(); for (Object o : command.getParameters()) { params.put(o); } json.put("parameters", params); } catch (JSONException e) { throw new WebDriverException(e); } try { // Force encoding as UTF-8. byte[] bytes = json.toString().getBytes("UTF-8"); return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { // If UTF-8 is missing from Java, we've got problems throw new IllegalStateException("Cannot convert string to UTF-8"); } }
public Response sendMessageAndWaitForResponse(Class<? extends RuntimeException> throwOnFailure, Command command) { String converted = convert(command); // Make this look like an HTTP request StringBuilder message = new StringBuilder(); message.append("GET / HTTP/1.1\n"); message.append("Host: localhost\n"); message.append("Content-Length: "); message.append(converted.length()).append("\n\n"); message.append(converted).append("\n"); try { out.write(message.toString()); out.flush(); } catch (IOException e) { throw new WebDriverException(e); } return waitForResponseFor(command.getCommandName()); }
private String sendMessage(Class<? extends RuntimeException> throwOnFailure, String methodName, Object... parameters) { return parent.sendMessage(throwOnFailure, new Command(parent.context, elementId, methodName, parameters)); }
public void quit() { try { sendMessageAndWaitForResponse(WebDriverException.class, new Command(null, "quit")); } catch (Exception e) { // Expected } allowFirefoxToQuit(); }
public Object executeScript(String script, Object... args) { // Escape the quote marks script = script.replaceAll("\"", "\\\""); Object[] convertedArgs = convertToJsObjects(args); Command command = new Command(context, null, "executeScript", script, convertedArgs); Response response = extension.sendMessageAndWaitForResponse(WebDriverException.class, command); context = response.getContext(); response.ifNecessaryThrow(WebDriverException.class); if ("NULL".equals(response.getExtraResult("resultType"))) return null; String resultType = (String) response.getExtraResult("resultType"); if ("ELEMENT".equals(resultType)) return new FirefoxWebElement(this, response.getResponseText()); Object result = response.getExtraResult("response"); if (result instanceof Integer) return new Long(response.getResponseText()); return result; }
public void quit() { try { sendMessageAndWaitForResponse(WebDriverException.class, new Command(null, "quit")); } catch (Exception e) { // this is expected } if (Platform.getCurrent().is(Platform.WINDOWS)) { quitOnWindows(); } else { quitOnOtherPlatforms(); } profile.clean(); }