List of usage examples for org.openqa.selenium.remote Response getStatus
public Integer getStatus()
From source file:com.jobaline.uiautomation.framework.selenium.phantomJsThreeHourTimeoutFix.HttpCommandExecutor.java
License:Apache License
private Response createResponse(HttpResponse httpResponse, HttpContext context, EntityWithEncoding entityWithEncoding) throws IOException { final Response response; Header header = httpResponse.getFirstHeader("Content-Type"); if (header != null && header.getValue().startsWith("application/json")) { String responseAsText = entityWithEncoding.getContentString(); try {/*from w w w.java 2 s.c o m*/ response = new JsonToBeanConverter().convert(Response.class, responseAsText); } catch (ClassCastException e) { if (responseAsText != null && "".equals(responseAsText)) { // The remote server has died, but has already set some headers. // Normally this occurs when the final window of the firefox driver // is closed on OS X. Return null, as the return value _should_ be // being ignored. This is not an elegant solution. return null; } throw new WebDriverException("Cannot convert text to response: " + responseAsText, e); } } else { response = new Response(); if (header != null && header.getValue().startsWith("image/png")) { response.setValue(entityWithEncoding.getContent()); } else if (entityWithEncoding.hasEntityContent()) { response.setValue(entityWithEncoding.getContentString()); } HttpHost finalHost = (HttpHost) context.getAttribute(HTTP_TARGET_HOST); String uri = finalHost.toURI(); String sessionId = HttpSessionId.getSessionId(uri); if (sessionId != null) { response.setSessionId(sessionId); } int statusCode = httpResponse.getStatusLine().getStatusCode(); if (!(statusCode > 199 && statusCode < 300)) { // 4xx represents an unknown command or a bad request. if (statusCode > 399 && statusCode < 500) { response.setStatus(ErrorCodes.UNKNOWN_COMMAND); } else if (statusCode > 499 && statusCode < 600) { // 5xx represents an internal server error. The response status should already be set, but // if not, set it to a general error code. if (response.getStatus() == ErrorCodes.SUCCESS) { response.setStatus(ErrorCodes.UNHANDLED_ERROR); } } else { response.setStatus(ErrorCodes.UNHANDLED_ERROR); } } if (response.getValue() instanceof String) { // We normalise to \n because Java will translate this to \r\n // if this is suitable on our platform, and if we have \r\n, java will // turn this into \r\r\n, which would be Bad! response.setValue(((String) response.getValue()).replace("\r\n", "\n")); } } response.setState(errorCodes.toState(response.getStatus())); return response; }
From source file:org.uiautomation.ios.server.servlet.IOSServlet.java
License:Apache License
private void process(HttpServletRequest request, HttpServletResponse response) throws Exception { WebDriverLikeRequest req = new WebDriverLikeRequest(request); response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); try {// w w w .j a va2 s.c om response.setStatus(200); Response resp = getResponse(req); // TODO implement the json protocol properly. if (req.getGenericCommand() == WebDriverLikeCommand.NEW_SESSION && resp.getStatus() == 0) { response.setStatus(301); String session = resp.getSessionId(); if (session == null || session.isEmpty()) { response.setStatus(500); } String scheme = request.getScheme(); // http String serverName = request.getServerName(); // hostname.com int serverPort = request.getServerPort(); // 80 String contextPath = request.getContextPath(); // /mywebapp // Reconstruct original requesting URL String url = scheme + "://" + serverName + ":" + serverPort + contextPath; response.setHeader("location", url + "/session/" + session); } // String s = toString(resp); BeanToJsonConverter convertor = new BeanToJsonConverter(); String s = convertor.convert(resp); // status is also used for debugging, it's worth formatting it nice. if (req.getGenericCommand() == WebDriverLikeCommand.STATUS) { JSONObject o = new JSONObject(s); response.getWriter().print(o.toString(2)); } else { response.getWriter().print(s); } } catch (WebDriverException e) { response.setStatus(500); response.getWriter().print(serializeException(e)); throw new WebDriverException("Error processing response." + e.getMessage(), e); } finally { response.getWriter().close(); } }
From source file:org.uiautomation.ios.server.servlet.IOSServlet.java
License:Apache License
private String toString(Response r) throws Exception { JSONObject o = new JSONObject(); o.put("sessionId", r.getSessionId()); o.put("status", r.getStatus()); o.put("value", r.getValue().toString()); return o.toString(); }
From source file:sf.wicklet.test.support.SeleniumTestUtil.java
License:Apache License
public static void debugprint(final FirefoxDriver driver, final boolean debug, final StepWatch timer, final String url, final Response response) { if (debug) {/*from w ww.ja va 2s . c o m*/ System.out.println("###"); System.out.println("### " + url); System.out.println("###"); System.out.println(timer.toString("Page title is: " + driver.getTitle())); System.out.println(response.getValue()); System.out.println("# sessionId: " + response.getSessionId()); System.out.println("# status: " + response.getStatus()); } }