public Object callExtension(String extensionMethod, Map<String, ?> parameters) { Map<String, Object> paramsWithHandler = Maps.newHashMap(); paramsWithHandler.putAll(parameters); paramsWithHandler.put("handlerName", extensionMethod); Response response = execute("selendroid-handleByExtension", paramsWithHandler); return response.getValue(); }
@Override public Map<String, Object> getConfiguration(DriverCommand command) { Response response = execute("selendroid-getCommandConfiguration", ImmutableMap.of("command", command.command)); return (Map<String, Object>) response.getValue(); }
@Override public Response handle() throws Exception { // Handle the case where the client does not send any desired capabilities. sessionId = allSessions.newSession( desiredCapabilities != null ? desiredCapabilities : new DesiredCapabilities()); Map<String, Object> capabilities = Maps.newHashMap(allSessions.get(sessionId).getCapabilities().asMap()); // Only servers implementing the server-side webdriver-backed selenium need // to return this particular value capabilities.put("webdriver.remote.sessionid", sessionId.toString()); if (desiredCapabilities != null) { LoggingManager.perSessionLogHandler() .configureLogging( (LoggingPreferences) desiredCapabilities.getCapability(CapabilityType.LOGGING_PREFS)); } LoggingManager.perSessionLogHandler().attachToCurrentThread(sessionId); Response response = new Response(); response.setSessionId(sessionId.toString()); response.setValue(capabilities); return response; }
@Test public void decodeNonJsonResponse_204() throws JSONException { HttpResponse response = new HttpResponse(); response.setStatus(HTTP_NO_CONTENT); Response decoded = codec.decode(response); assertEquals(ErrorCodes.SUCCESS, decoded.getStatus()); assertNull(decoded.getValue()); }
@Test public void decodeNonJsonResponse_5xx() throws JSONException { HttpResponse response = new HttpResponse(); response.setStatus(HTTP_INTERNAL_ERROR); response.setContent("foobar".getBytes(UTF_8)); Response decoded = codec.decode(response); assertEquals(ErrorCodes.UNHANDLED_ERROR, decoded.getStatus()); assertEquals("foobar", decoded.getValue()); }
@Test public void decodeNonJsonResponse_200() throws JSONException { HttpResponse response = new HttpResponse(); response.setStatus(HTTP_OK); response.setContent("foobar".getBytes(UTF_8)); Response decoded = codec.decode(response); assertEquals(ErrorCodes.SUCCESS, decoded.getStatus()); assertEquals("foobar", decoded.getValue()); }
@Test public void decodeUtf16EncodedResponse() { HttpResponse httpResponse = new HttpResponse(); httpResponse.setStatus(200); httpResponse.setHeader(CONTENT_TYPE, JSON_UTF_8.withCharset(UTF_16).toString()); httpResponse.setContent("{\"status\":0,\"value\":\"水\"}".getBytes(UTF_16)); Response response = codec.decode(httpResponse); assertEquals("水", response.getValue()); }
@Test public void decodeNonJsonResponse_4xx() throws JSONException { HttpResponse response = new HttpResponse(); response.setStatus(HTTP_BAD_REQUEST); response.setContent("foobar".getBytes(UTF_8)); Response decoded = codec.decode(response); assertEquals(ErrorCodes.UNKNOWN_COMMAND, decoded.getStatus()); assertEquals("foobar", decoded.getValue()); }
/* * (non-Javadoc) * * @see org.uiautomation.ios.server.command.Handler#handle() */ @Override public Response handle() throws Exception { JSONObject res = new JSONObject(); res.put( "os", new JSONObject() .put("name", System.getProperty("os.name")) .put("arch", System.getProperty("os.arch")) .put("version", System.getProperty("os.version"))); res.put("java", new JSONObject().put("version", System.getProperty("java.version"))); res.put("ios", new JSONObject().put("simulatorVersion", ClassicCommands.getDefaultSDK())); JSONArray supportedApps = new JSONArray(); for (IOSApplication a : getDriver().getSupportedApplications()) { JSONObject app = new JSONObject(); JSONObject resources = new JSONObject(); for (String key : a.getResources().keySet()) { resources.put(key, "/wd/hub/resources/" + getDriver().getCache().getKey(a, key)); } app.put("resources", resources); Map<String, Object> capabilities = getDriver().getCapabilities(a).getRawCapabilities(); for (String key : capabilities.keySet()) { app.put(key, capabilities.get(key)); } supportedApps.put(app); } res.put("supportedApps", supportedApps); res.put( "build", new JSONObject() .put("version", BuildInfo.getAttribute("version")) .put("time", BuildInfo.getAttribute("buildTimestamp")) .put("revision", BuildInfo.getAttribute("sha"))); List<ServerSideSession> sessions = getDriver().getSessions(); Response resp = new Response(); resp.setStatus(0); resp.setValue(res); if (sessions.size() == 0) { resp.setSessionId(null); } else if (sessions.size() == 1) { resp.setSessionId(sessions.get(0).getSessionId()); } else { throw new WebDriverException("NI multi sessions per server."); } return resp; }
@Override public Set<String> getContextHandles() { Response response = execute(org.openqa.selenium.remote.DriverCommand.GET_CONTEXT_HANDLES); Object value = response.getValue(); try { List<String> returnedValues = (List<String>) value; return new LinkedHashSet<String>(returnedValues); } catch (ClassCastException ex) { throw new WebDriverException( "Returned value cannot be converted to List<String>: " + value, ex); } }
@Test public void roundTrip() throws JSONException { Response response = new Response(); response.setStatus(ErrorCodes.SUCCESS); response.setValue(ImmutableMap.of("color", "red")); HttpResponse httpResponse = codec.encode(response); Response decoded = codec.decode(httpResponse); assertEquals(response.getStatus(), decoded.getStatus()); assertEquals(response.getSessionId(), decoded.getSessionId()); assertEquals(response.getValue(), decoded.getValue()); }
@Test public void decodeJsonResponseMissingContentType() throws JSONException { Response response = new Response(); response.setStatus(ErrorCodes.ASYNC_SCRIPT_TIMEOUT); response.setValue(ImmutableMap.of("color", "red")); HttpResponse httpResponse = new HttpResponse(); httpResponse.setStatus(HTTP_OK); httpResponse.setContent(new BeanToJsonConverter().convert(response).getBytes(UTF_8)); Response decoded = codec.decode(httpResponse); assertEquals(response.getStatus(), decoded.getStatus()); assertEquals(response.getSessionId(), decoded.getSessionId()); assertEquals(response.getValue(), decoded.getValue()); }
@Override public Response handle() throws Exception { boolean useNativeEvents = getConfiguration("nativeEvents", nativeEvents); if (useNativeEvents) { // backNative(); } else { backWeb(); } getSession().getRemoteWebDriver().getContext().newContext(); getSession().getRemoteWebDriver().waitForPageToLoad(); Response resp = new Response(); resp.setSessionId(getSession().getSessionId()); resp.setStatus(0); resp.setValue(new JSONObject()); return resp; }
@Override public Response handle() throws Exception { JSONObject payload = getRequest().getPayload(); String type = payload.getString("using"); String value = payload.getString("value"); RemoteWebElement element = null; if (getRequest().hasVariable(":reference")) { int id = Integer.parseInt(getRequest().getVariableValue(":reference")); element = new RemoteWebElement(new NodeId(id), getSession()); } else { element = getSession().getWebInspector().getDocument(); } List<RemoteWebElement> res; if ("link text".equals(type)) { res = element.findElementsByLinkText(value, false); } else if ("partial link text".equals(type)) { res = element.findElementsByLinkText(value, true); } else if ("xpath".equals(type)) { res = element.findElementsByXpath(value); } else { String cssSelector = ToCSSSelectorConvertor.convertToCSSSelector(type, value); res = element.findElementsByCSSSelector(cssSelector); } JSONArray array = new JSONArray(); List<JSONObject> list = new ArrayList<JSONObject>(); for (RemoteWebElement el : res) { list.add(new JSONObject().put("ELEMENT", "" + el.getNodeId().getId())); } Response resp = new Response(); resp.setSessionId(getSession().getSessionId()); resp.setStatus(0); resp.setValue(list); return resp; }
@Override public Response handle() throws Exception { JSONArray res = new JSONArray(); List<ServerSideSession> sessions = getDriver().getSessions(); for (ServerSideSession s : sessions) { JSONObject session = new JSONObject(); session.put("id", s.getSessionId()); IOSApplication app = s.getApplication(); IOSCapabilities cap = getDriver().getCapabilities(app); session.put("capabilities", cap.getRawCapabilities()); res.put(session); } Response resp = new Response(); resp.setSessionId("dummy one"); resp.setStatus(0); resp.setValue(res.toString()); return resp; }
/** {@inheritDoc} */ @Override public int getBrightness() { Response response = execute("selendroid-getBrightness"); Number value = (Number) response.getValue(); return value.intValue(); }
public ResultType call() throws Exception { response = newResponse(); response.setValue(getElement().getAttribute(name)); return ResultType.SUCCESS; }
@Test public void convertsResponses_failure() throws JSONException { Response response = new Response(); response.setStatus(ErrorCodes.NO_SUCH_ELEMENT); response.setValue(ImmutableMap.of("color", "red")); HttpResponse converted = codec.encode(response); assertThat(converted.getStatus(), is(HTTP_INTERNAL_ERROR)); assertThat(converted.getHeader(CONTENT_TYPE), is(JSON_UTF_8.toString())); Response rebuilt = new JsonToBeanConverter() .convert(Response.class, new String(converted.getContent(), UTF_8)); assertEquals(response.getStatus(), rebuilt.getStatus()); assertEquals(response.getState(), rebuilt.getState()); assertEquals(response.getSessionId(), rebuilt.getSessionId()); assertEquals(response.getValue(), rebuilt.getValue()); }