Example usage for org.openqa.selenium Proxy setSocksProxy

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

Introduction

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

Prototype

public Proxy setSocksProxy(String socksProxy) 

Source Link

Document

Specifies which proxy to use for SOCKS.

Usage

From source file:com.qwazr.crawler.web.driver.BrowserDriverBuilder.java

License:Apache License

public BrowserDriver build() throws ReflectiveOperationException, SecurityException {
    BrowserDriverEnum browserType = BrowserDriverEnum.html_unit;

    final WebCrawlDefinition.ProxyDefinition proxyDef;

    DesiredCapabilities capabilities = null;

    if (crawlDefinition != null) {

        // Choose a browser type
        if (crawlDefinition.browser_type != null)
            browserType = crawlDefinition.browser_type;

        proxyDef = selectProxy();/*from   w w  w .ja v  a  2s.c  o  m*/

        // Setup the proxy
        if (proxyDef != null) {
            capabilities = checkCapabilities(capabilities);
            org.openqa.selenium.Proxy proxy = new Proxy();
            if (proxyDef.http_proxy != null)
                proxy.setHttpProxy(proxyDef.http_proxy);
            if (proxyDef.ftp_proxy != null)
                proxy.setFtpProxy(proxyDef.ftp_proxy);
            if (proxyDef.ssl_proxy != null)
                proxy.setSslProxy(proxyDef.ssl_proxy);
            if (proxyDef.socks_proxy != null)
                proxy.setSocksProxy(proxyDef.socks_proxy);
            if (proxyDef.socks_username != null)
                proxy.setSocksUsername(proxyDef.socks_username);
            if (proxyDef.socks_password != null)
                proxy.setSocksPassword(proxyDef.socks_password);
            if (proxyDef.no_proxy != null)
                proxy.setNoProxy(proxyDef.no_proxy);
            if (proxyDef.proxy_autoconfig_url != null)
                proxy.setProxyAutoconfigUrl(proxyDef.proxy_autoconfig_url);
            capabilities.setCapability(CapabilityType.PROXY, proxy);
        }

        // Setup the language
        if (crawlDefinition.browser_language != null) {
            capabilities = checkCapabilities(capabilities);
            capabilities.setCapability(AdditionalCapabilities.QWAZR_BROWSER_LANGUAGE,
                    crawlDefinition.browser_language);
            if (browserType == BrowserDriverEnum.phantomjs)
                capabilities.setCapability(
                        PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language",
                        crawlDefinition.browser_language);
        }

        // Download images
        if (crawlDefinition.download_images != null) {
            capabilities = checkCapabilities(capabilities);
            if (browserType == BrowserDriverEnum.phantomjs)
                capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "loadImages",
                        crawlDefinition.download_images);
        }

        // Web security
        if (crawlDefinition.web_security != null) {
            capabilities = checkCapabilities(capabilities);
            if (browserType == BrowserDriverEnum.phantomjs)
                capabilities.setCapability(
                        PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "webSecurityEnabled",
                        crawlDefinition.web_security);
        }

        // Choose a browser name
        if (crawlDefinition.browser_name != null) {
            capabilities = checkCapabilities(capabilities);
            capabilities.setBrowserName(crawlDefinition.browser_name);
        }

        if (crawlDefinition.browser_version != null) {
            capabilities = checkCapabilities(capabilities);
            capabilities.setVersion(crawlDefinition.browser_version);
        }

        // Javascript capability
        if (crawlDefinition.javascript_enabled != null) {
            capabilities = checkCapabilities(capabilities);
            capabilities.setJavascriptEnabled(crawlDefinition.javascript_enabled);
            if (browserType == BrowserDriverEnum.phantomjs)
                capabilities.setCapability(
                        PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "javascriptEnabled",
                        crawlDefinition.javascript_enabled);

        }

    } else
        proxyDef = null;

    if (browserType == BrowserDriverEnum.phantomjs) {
        capabilities = checkCapabilities(capabilities);
        capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
                new String[] { "--webdriver-loglevel=NONE", "--ignore-ssl-errors=true" });
    }

    final WebDriver driver = browserType.getNewInstance(capabilities);
    try {
        final BrowserDriver browserDriver = new BrowserDriver(browserType, driver, proxyDef);
        browserDriver.setTimeouts(crawlDefinition.implicitly_wait, crawlDefinition.page_load_timeout,
                crawlDefinition.script_timeout);

        if (crawlDefinition.cookies != null)
            for (Map.Entry<String, String> cookie : crawlDefinition.cookies.entrySet())
                driver.manage().addCookie(new Cookie(cookie.getKey(), cookie.getValue()));

        return browserDriver;
    } catch (Exception e) {
        driver.quit();
        throw e;
    }
}

From source file:org.suren.autotest.web.framework.selenium.CapabilityConfig.java

License:Apache License

/**
 * ?/*from   ww  w . j  a va 2s.co  m*/
 * chrome://version/
 */
private void chrome() {
    DesiredCapabilities capability = DesiredCapabilities.chrome();

    ChromeOptions options = new ChromeOptions();
    Iterator<Object> chromeKeys = enginePro.keySet().iterator();
    Proxy proxy = new Proxy();
    while (chromeKeys.hasNext()) {
        String key = chromeKeys.next().toString();
        if (!key.startsWith("chrome")) {
            continue;
        }

        if (key.startsWith("chrome.args")) {
            String arg = key.replace("chrome.args.", "") + "=" + enginePro.getProperty(key);
            if (arg.endsWith("=")) {
                arg = arg.substring(0, arg.length() - 1);
            }
            options.addArguments(arg);
            logger.info(String.format("chrome arguments : [%s]", arg));
        } else if (key.startsWith("chrome.cap.proxy.http")) {
            String val = enginePro.getProperty(key);

            proxy.setHttpProxy(val);
        } else if (key.startsWith("chrome.cap.proxy.ftp")) {
            String val = enginePro.getProperty(key);

            proxy.setFtpProxy(val);
        } else if (key.startsWith("chrome.cap.proxy.socks")) {
            String val = enginePro.getProperty(key);

            proxy.setSocksProxy(val);
        } else if (key.startsWith("chrome.cap.proxy.socks.username")) {
            String val = enginePro.getProperty(key);

            proxy.setSocksUsername(val);
        } else if (key.startsWith("chrome.cap.proxy.socks.password")) {
            String val = enginePro.getProperty(key);

            proxy.setSocksPassword(val);
        } else if (key.startsWith("chrome.binary")) {
            options.setBinary(enginePro.getProperty(key));
        }
    }

    if ("true".equals(enginePro.getProperty("chrome.cap.proxy.enable"))) {
        capability.setCapability("proxy", proxy);
    }
    capability.setCapability(ChromeOptions.CAPABILITY, options);

    engineCapMap.put(DRIVER_CHROME, capability);
}