List of usage examples for org.openqa.selenium.json Json Json
Json
From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java
License:Apache License
/** * Creates instance of {@link NewAppiumSessionPayload}. * * @param caps capabilities to create a new session * @return instance of {@link NewAppiumSessionPayload} *//*from w w w.j a v a2 s . com*/ public static NewAppiumSessionPayload create(Capabilities caps) throws IOException { boolean forceMobileJSONWP = ofNullable(caps.getCapability(FORCE_MJSONWP)) .map(o -> Boolean.class.isAssignableFrom(o.getClass()) && Boolean.class.cast(o)).orElse(false); HashMap<String, ?> capabilityMap = new HashMap<>(caps.asMap()); capabilityMap.remove(FORCE_MJSONWP); Map<String, ?> source = of(DESIRED_CAPABILITIES, capabilityMap); String json = new Json().toJson(source); return new NewAppiumSessionPayload(new StringReader(json), forceMobileJSONWP); }
From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java
License:Apache License
/** * Writes json capabilities to some appendable object. * * @param appendable to write a json// w w w . ja va 2s. com */ public void writeTo(Appendable appendable) throws IOException { try (JsonOutput json = new Json().newOutput(appendable)) { json.beginObject(); Map<String, Object> first = getOss(); if (first == null) { //noinspection unchecked first = (Map<String, Object>) stream().findFirst().orElse(new ImmutableCapabilities()).asMap(); } // Write the first capability we get as the desired capability. json.name(DESIRED_CAPABILITIES); json.write(first); if (!forceMobileJSONWP) { // And write the first capability for gecko13 json.name(CAPABILITIES); json.beginObject(); // Then write everything into the w3c payload. Because of the way we do this, it's easiest // to just populate the "firstMatch" section. The spec says it's fine to omit the // "alwaysMatch" field, so we do this. json.name(FIRST_MATCH); json.beginArray(); getW3C().forEach(json::write); json.endArray(); json.endObject(); // Close "capabilities" object } writeMetaData(json); json.endObject(); } }
From source file:org.openqa.grid.e2e.misc.GridListActiveSessionsTest.java
License:Apache License
private Map<String, Object> getSessions(Hub hub) throws IOException { String url = String.format("http://%s:%d/grid/api/sessions", hub.getUrl().getHost(), hub.getUrl().getPort());//from w w w. ja va 2 s. co m URL grid = new URL(url); URLConnection connection = grid.openConnection(); try (InputStream in = connection.getInputStream(); JsonInput input = new Json().newInput(new BufferedReader(new InputStreamReader(in)))) { return input.read(Json.MAP_TYPE); } }
From source file:org.openqa.grid.internal.cli.GridHubCliOptions.java
License:Apache License
private static IDefaultProvider defaults(String json) { Map<String, Object> map = (Map<String, Object>) new Json().toType(json, Map.class); map.remove("custom"); return optionName -> { String option = optionName.replaceAll("-", ""); return map.containsKey(option) && map.get(option) != null ? map.get(option).toString() : null; };// w w w . j a v a 2s . c om }
From source file:org.openqa.grid.internal.ExternalSessionKey.java
License:Apache License
/** * Extract the external key from the server response for a selenium2 new session request. * The response body is expected to be of the form {"status":0,"sessionId":"XXXX",...}. * @param responseBody the response body to parse * @return the extracted ExternalKey, or null if one was not found. *///from www .ja v a 2 s.c o m public static ExternalSessionKey fromJsonResponseBody(String responseBody) { try { Map<String, Object> json = new Json().toType(responseBody, MAP_TYPE); if (json.get("sessionId") instanceof String) { return new ExternalSessionKey((String) json.get("sessionId")); } // W3C response if (json.get("value") instanceof Map) { Map<?, ?> value = (Map<?, ?>) json.get("value"); if (value.get("sessionId") instanceof String) { return new ExternalSessionKey((String) value.get("sessionId")); } } } catch (JsonException | ClassCastException e) { return null; } return null; }
From source file:org.openqa.grid.internal.utils.configuration.GridHubConfigurationTest.java
License:Apache License
@Test public void testLoadFromJson() throws IOException { GridHubConfiguration ghc;// ww w . java2 s . co m try (Reader reader = new StringReader("{\"role\":\"hub\", \"host\":\"dummyhost\", \"port\":1234}"); JsonInput jsonInput = new Json().newInput(reader)) { ghc = GridHubConfiguration.loadFromJSON(jsonInput); } assertThat(ghc.role).isEqualTo("hub"); assertThat(ghc.port).isEqualTo(1234); assertThat(ghc.host).isEqualTo("dummyhost"); assertThat(ghc.newSessionWaitTimeout).isEqualTo(-1); }
From source file:org.openqa.grid.internal.utils.configuration.json.CommonJsonConfiguration.java
License:Apache License
private static JsonInput loadJsonFromResourceOrFile(String source) { try {// ww w .j av a 2s . c o m return new Json().newInput(readFileOrResource(source)); } catch (RuntimeException e) { throw new GridConfigurationException("Unable to load configuration from " + source, e); } }
From source file:org.openqa.grid.internal.utils.configuration.StandaloneConfiguration.java
License:Apache License
/** * load a JSON file from the resource or file system. As a fallback, treats {@code resource} as a * JSON string to be parsed.//from w w w. ja v a 2 s . c om * * @param resource file or jar resource location * @return A JsonObject representing the passed resource argument. */ protected static JsonInput loadJsonFromResourceOrFile(String resource) { try { return new Json().newInput(readFileOrResource(resource)); } catch (RuntimeException e) { throw new GridConfigurationException("Unable to read input", e); } }
From source file:org.openqa.grid.web.servlet.BaseServletTest.java
License:Apache License
protected static FakeHttpServletResponse sendCommand(HttpServlet servlet, String method, String commandPath, Map<String, Object> parameters) throws IOException, ServletException { FakeHttpServletRequest request = new FakeHttpServletRequest(method, createUrl(commandPath)); if ("get".equalsIgnoreCase(method) && parameters != null) { Map<String, String> params = new HashMap<>(); for (Map.Entry<String, Object> parameter : parameters.entrySet()) { params.put(parameter.getKey(), parameter.getValue().toString()); }/* w ww . j ava 2s . co m*/ request.setParameters(params); } if ("post".equalsIgnoreCase(method) && parameters != null) { request.setBody(new Json().toJson(parameters)); } FakeHttpServletResponse response = new FakeHttpServletResponse(); servlet.service(request, response); return response; }
From source file:org.openqa.grid.web.servlet.DisplayHelpHandlerTest.java
License:Apache License
@Before public void setUp() { handler = new DisplayHelpHandler(new Json(), GridRole.NOT_GRID, "/wd/hub"); }