Example usage for org.openqa.selenium Proxy getSocksUsername

List of usage examples for org.openqa.selenium Proxy getSocksUsername

Introduction

In this page you can find the example usage for org.openqa.selenium Proxy getSocksUsername.

Prototype

public String getSocksUsername() 

Source Link

Document

Gets the SOCKS proxy's username.

Usage

From source file:phantomjs.PhantomJSDriver.java

License:Apache License

/**
 * In case this instance was required to handle the life-cycle of the PhantomJS process, that starts here.
 * Otherwise this is a "transparent" method.
 *
 * @param desiredCapabilities//from w w w. j a  v a  2  s.co m
 * @param requiredCapabilities
 * @throws WebDriverException
 */
@Override
protected void startSession(Capabilities desiredCapabilities, Capabilities requiredCapabilities)
        throws WebDriverException {
    // Will launch a PhantomJS WebDriver process ONLY if this driver is not already using an external one
    if (!useExternalPhantomJS) {
        // Read PhantomJS executable and JS Driver path from the capabilities
        String executablePath = (String) desiredCapabilities.getCapability(CAPABILITY_PHANTOMSJS_EXEC_PATH);
        String driverPath = (String) desiredCapabilities.getCapability(CAPABILITY_PHANTOMJS_DRIVER_PATH);

        // Throw WebDriverException in case any of the previous two was not provided
        if (executablePath == null || driverPath == null || !new File(executablePath).exists()
                || !new File(driverPath).exists()) {
            throw new WebDriverException(
                    "PhantomJSDriver: Path to PhantomJS Executable or Driver not provided/invalid");
        }

        // Read the Proxy configuration
        Proxy proxy = (Proxy) desiredCapabilities.getCapability(CapabilityType.PROXY);
        // Prepare the parameters to pass to the PhantomJS executable on the command line
        String proxyParams = "";
        if (proxy != null) {
            switch (proxy.getProxyType()) {
            case MANUAL:
                if (!proxy.getHttpProxy().isEmpty()) { //< HTTP proxy
                    log(getSessionId(), "Reading Proxy Configuration", "Manual, HTTP", When.BEFORE);
                    proxyParams = String.format("--proxy-type=http --proxy=%s", proxy.getHttpProxy());
                } else if (!proxy.getSocksProxy().isEmpty()) { //< SOCKS5 proxy
                    log(getSessionId(), "Reading Proxy Configuration", "Manual, SOCKS5", When.BEFORE);
                    proxyParams = String.format("--proxy-type=socks5 --proxy=%s --proxy-auth=%s:%s",
                            proxy.getSocksProxy(), proxy.getSocksUsername(), proxy.getSocksPassword());
                } else {
                    // TODO Other type of proxy not supported yet by PhantomJS
                    log(getSessionId(), "Reading Proxy Configuration", "Manual, NOT SUPPORTED", When.BEFORE);
                }
                break;
            case PAC:
                // TODO Not supported yet by PhantomJS
                log(getSessionId(), "Reading Proxy Configuration", "PAC, NOT SUPPORTED", When.BEFORE);
                break;
            case SYSTEM:
                log(getSessionId(), "Reading Proxy Configuration", "SYSTEM", When.BEFORE);
                proxyParams = "--proxy-type=system";
                break;
            case AUTODETECT:
                // TODO Not supported yet by PhantomJS
                log(getSessionId(), "Reading Proxy Configuration", "AUTODETECT, NOT SUPPORTED", When.BEFORE);
                break;
            case DIRECT:
                log(getSessionId(), "Reading Proxy Configuration", "DIRECT", When.BEFORE);
                proxyParams = "--proxy-type=none";
            default:
                log(getSessionId(), "Reading Proxy Configuration", "NONE", When.BEFORE);
                proxyParams = "";
                break;
            }
        }

        // Find a free port to launch PhantomJS WebDriver on
        String phantomJSPortStr = Integer.toString(PortProber.findFreePort());
        log(getSessionId(), "Looking for a free port for PhantomJS WebDriver", phantomJSPortStr, When.AFTER);

        log(getSessionId(), "About to launch PhantomJS WebDriver", null, When.BEFORE);
        try {
            // Launch PhantomJS and wait for first output on console before proceeding
            phantomJSProcess = Runtime.getRuntime().exec(new String[] { executablePath, //< path to PhantomJS executable
                    proxyParams, //< command line parameters for Proxy configuration
                    driverPath, //< path to the PhantomJS Driver
                    phantomJSPortStr //< port on which the Driver should listen on
            });
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(phantomJSProcess.getInputStream()));
            while (reader.readLine() == null) {
                /* wait here for some output: once it prints, it's ready to work */ }
            useExternalPhantomJS = false;

            // PhantomJS is ready to serve.
            // Setting the HTTP Command Executor that this RemoteWebDriver will use
            setCommandExecutor(new HttpCommandExecutor(new URL("http://localhost:" + phantomJSPortStr)));
        } catch (IOException ioe) {
            // Log exception & Cleanup
            log(getSessionId(), null, ioe, When.EXCEPTION);
            stopClient();
            throw new WebDriverException("PhantomJSDriver: " + ioe.getMessage(), ioe);
        }
        log(getSessionId(), "PhantomJS WebDriver ready", null, When.AFTER);
    }

    // We are ready to let the RemoteDriver do its job from here
    super.startSession(desiredCapabilities, requiredCapabilities);
}