public void testShouldConvertAProxyCorrectly() throws JSONException { Proxy proxy = new Proxy(); proxy.setHttpProxy("localhost:4444"); DesiredCapabilities caps = new DesiredCapabilities("foo", "1", Platform.LINUX); caps.setCapability(CapabilityType.PROXY, proxy); Map<String, ?> asMap = ImmutableMap.of("desiredCapabilities", caps); Command command = new Command(new SessionId("empty"), DriverCommand.NEW_SESSION, asMap); String json = new BeanToJsonConverter().convert(command.getParameters()); JSONObject converted = new JSONObject(json); JSONObject capsAsMap = converted.getJSONObject("desiredCapabilities"); assertEquals(json, proxy.getHttpProxy(), capsAsMap.getJSONObject("proxy").get("httpProxy")); }
/** * Creates a new service. Before creating a new service, the builder will find a port for * the server to listen to. * * @return The new service object. */ public PhantomJSDriverService build() { // Find a port to listen on, if not already decided port = port == 0 ? PortProber.findFreePort() : port; // Few final checks checkState(phantomjs != null, "Path to PhantomJS executable not specified"); try { // Build a list of command line arguments for the executable ImmutableList.Builder<String> argsBuilder = ImmutableList.builder(); // Add command line proxy configuration for PhantomJS if (proxy != null) { switch (proxy.getProxyType()) { case MANUAL: if (proxy.getHttpProxy() != null && !proxy.getHttpProxy().isEmpty()) { //< HTTP proxy argsBuilder.add("--proxy-type=http"); argsBuilder.add(String.format("--proxy=%s", proxy.getHttpProxy())); } else if (proxy.getSocksProxy() != null && !proxy.getSocksProxy().isEmpty()) { //< SOCKS5 proxy argsBuilder.add("--proxy-type=socks5"); argsBuilder.add(String.format("--proxy=%s", proxy.getSocksProxy())); if (proxy.getSocksUsername() != null && !proxy.getSocksUsername().isEmpty() && proxy.getSocksPassword() != null && !proxy.getSocksPassword().isEmpty()) { argsBuilder.add(String.format("--proxy-auth=%s:%s", proxy.getSocksUsername(), proxy.getSocksPassword())); } } else { // TODO Not supported yet by PhantomJS checkArgument(true, "PhantomJS supports only HTTP and Socks5 Proxy currently"); } break; case PAC: // TODO Not supported yet by PhantomJS checkArgument(true, "PhantomJS doesn't support Proxy PAC files"); break; case SYSTEM: argsBuilder.add("--proxy-type=system"); break; case AUTODETECT: // TODO Not supported yet by PhantomJS checkArgument(true, "PhantomJS doesn't support Proxy Auto-configuration"); break; case DIRECT: default: argsBuilder.add("--proxy-type=none"); break; } } if (this.acceptSslCerts) { argsBuilder.add("--web-security=false"); argsBuilder.add("--ssl-protocol=any"); argsBuilder.add("--ignore-ssl-errors=true"); } // Additional command line arguments (if provided) if (this.commandLineArguments != null) { argsBuilder.add(this.commandLineArguments); } // Should use an external GhostDriver? if (ghostdriver != null) { //< Path to GhostDriver provided: use it simply as a PhantomJS script // Add the canonical path to GhostDriver argsBuilder.add(ghostdriver.getCanonicalPath()); // Add the port to listen on (if not specified in command line args) if (!argsContains(this.ghostdriverCommandLineArguments, "port")) { argsBuilder.add(String.format("--port=%d", port)); } // Add Log File (if not specified in command line args) if (logFile != null && !argsContains(this.ghostdriverCommandLineArguments, "logFile")) { argsBuilder.add(String.format("--logFile=%s", logFile.getAbsolutePath())); } // Additional GhostDriver command line arguments (if provided) if (this.ghostdriverCommandLineArguments!= null) { argsBuilder.add(this.ghostdriverCommandLineArguments); } } else { //< Path to GhostDriver not provided: use PhantomJS's internal GhostDriver (default behaviour) // Append required parameters (if not specified in command line args) if (!argsContains(this.commandLineArguments, "webdriver")) { argsBuilder.add(String.format("--webdriver=%d", port)); } // Add Log File (if not specified in command line args) if (logFile != null && !argsContains(this.commandLineArguments, "webdriver-logfile")) { argsBuilder.add(String.format("--webdriver-logfile=%s", logFile.getAbsolutePath())); } } // Create a new service return new PhantomJSDriverService(phantomjs, port, argsBuilder.build(), environment); } catch (IOException e) { throw new WebDriverException(e); } }