Example usage for org.openqa.selenium Proxy setNoProxy

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

Introduction

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

Prototype

public Proxy setNoProxy(String noProxy) 

Source Link

Document

Sets proxy bypass (noproxy) addresses

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();//  ww  w.ja  v a  2 s  .  com

        // 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.asqatasun.sebuilder.tools.ProfileFactory.java

License:Open Source License

private void setUpProxy(FirefoxProfile firefoxProfile) {
    if (!StringUtils.isEmpty(proxyPort) && !StringUtils.isEmpty(proxyHost)) {
        StringBuilder strb = new StringBuilder(proxyHost);
        strb.append(":");
        strb.append(proxyPort);//from  ww  w  .ja v a2 s  . c  o  m
        Proxy proxy = new Proxy();
        proxy.setFtpProxy(strb.toString());
        proxy.setHttpProxy(strb.toString());
        proxy.setSslProxy(strb.toString());
        if (StringUtils.isNotEmpty(proxyExclusionUrl)) {
            proxy.setNoProxy(proxyExclusionUrl.replaceAll(";", ","));
        }
        firefoxProfile.setProxyPreferences(proxy);
    }
}

From source file:org.nuxeo.functionaltests.AbstractTest.java

License:Open Source License

protected static void initFirefoxDriver() throws Exception {
    DesiredCapabilities dc = DesiredCapabilities.firefox();
    FirefoxProfile profile = new FirefoxProfile();
    // Disable native events (makes things break on Windows)
    profile.setEnableNativeEvents(false);
    // Set English as default language
    profile.setPreference("general.useragent.locale", "en");
    profile.setPreference("intl.accept_languages", "en");
    // Set other confs to speed up FF

    // Speed up firefox by pipelining requests on a single connection
    profile.setPreference("network.http.keep-alive", true);
    profile.setPreference("network.http.pipelining", true);
    profile.setPreference("network.http.proxy.pipelining", true);
    profile.setPreference("network.http.pipelining.maxrequests", 8);

    // Try to use less memory
    profile.setPreference("browser.sessionhistory.max_entries", 10);
    profile.setPreference("browser.sessionhistory.max_total_viewers", 4);
    profile.setPreference("browser.sessionstore.max_tabs_undo", 4);
    profile.setPreference("browser.sessionstore.interval", 1800000);

    // do not load images
    profile.setPreference("permissions.default.image", 2);

    // disable unresponsive script alerts
    profile.setPreference("dom.max_script_run_time", 0);
    profile.setPreference("dom.max_chrome_script_run_time", 0);

    // don't skip proxy for localhost
    profile.setPreference("network.proxy.no_proxies_on", "");

    // prevent different kinds of popups/alerts
    profile.setPreference("browser.tabs.warnOnClose", false);
    profile.setPreference("browser.tabs.warnOnOpen", false);
    profile.setPreference("extensions.newAddons", false);
    profile.setPreference("extensions.update.notifyUser", false);

    // disable autoscrolling
    profile.setPreference("browser.urlbar.autocomplete.enabled", false);

    // downloads conf
    profile.setPreference("browser.download.useDownloadDir", false);

    // prevent FF from running in offline mode when there's no network
    // connection
    profile.setPreference("toolkit.networkmanager.disable", true);

    // prevent FF from giving health reports
    profile.setPreference("datareporting.policy.dataSubmissionEnabled", false);
    profile.setPreference("datareporting.healthreport.uploadEnabled", false);
    profile.setPreference("datareporting.healthreport.service.firstRun", false);
    profile.setPreference("datareporting.healthreport.service.enabled", false);
    profile.setPreference("datareporting.healthreport.logging.consoleEnabled", false);

    // start page conf to speed up FF
    profile.setPreference("browser.startup.homepage", "about:blank");
    profile.setPreference("pref.browser.homepage.disable_button.bookmark_page", false);
    profile.setPreference("pref.browser.homepage.disable_button.restore_default", false);

    // misc confs to avoid useless updates
    profile.setPreference("browser.search.update", false);
    profile.setPreference("browser.bookmarks.restore_default_bookmarks", false);

    // misc confs to speed up FF
    profile.setPreference("extensions.ui.dictionary.hidden", true);
    profile.setPreference("layout.spellcheckDefault", 0);

    addFireBug(profile);//from   w ww .  j a  v  a2s .c  o  m
    Proxy proxy = startProxy();
    if (proxy != null) {
        // Does not work, but leave code for when it does
        // Workaround: use 127.0.0.2
        proxy.setNoProxy("");
        profile.setProxyPreferences(proxy);
    }
    dc.setCapability(FirefoxDriver.PROFILE, profile);
    driver = new FirefoxDriver(dc);
}

From source file:org.nuxeo.functionaltests.AbstractTest.java

License:Open Source License

@SuppressWarnings("deprecation")
protected static void initChromeDriver() throws Exception {
    if (System.getProperty(SYSPROP_CHROME_DRIVER_PATH) == null) {
        String chromeDriverDefaultPath = null;
        String chromeDriverExecutableName = CHROME_DRIVER_DEFAULT_EXECUTABLE_NAME;
        if (SystemUtils.IS_OS_LINUX) {
            chromeDriverDefaultPath = CHROME_DRIVER_DEFAULT_PATH_LINUX;
        } else if (SystemUtils.IS_OS_MAC) {
            chromeDriverDefaultPath = CHROME_DRIVER_DEFAULT_PATH_MAC;
        } else if (SystemUtils.IS_OS_WINDOWS_XP) {
            chromeDriverDefaultPath = CHROME_DRIVER_DEFAULT_PATH_WINXP;
            chromeDriverExecutableName = CHROME_DRIVER_WINDOWS_EXECUTABLE_NAME;
        } else if (SystemUtils.IS_OS_WINDOWS_VISTA) {
            chromeDriverDefaultPath = CHROME_DRIVER_DEFAULT_PATH_WINVISTA;
            chromeDriverExecutableName = CHROME_DRIVER_WINDOWS_EXECUTABLE_NAME;
        } else if (SystemUtils.IS_OS_WINDOWS) {
            // Unknown default path on other Windows OS. To be completed.
            chromeDriverExecutableName = CHROME_DRIVER_WINDOWS_EXECUTABLE_NAME;
        }/*from w  w  w.j ava 2  s. c  o  m*/

        if (chromeDriverDefaultPath != null && new File(chromeDriverDefaultPath).exists()) {
            log.warn(String.format("Missing property %s but found %s. Using it...", SYSPROP_CHROME_DRIVER_PATH,
                    chromeDriverDefaultPath));
            System.setProperty(SYSPROP_CHROME_DRIVER_PATH, chromeDriverDefaultPath);
        } else {
            // Can't find chromedriver in default location, check system
            // path
            File chromeDriverExecutable = findExecutableOnPath(chromeDriverExecutableName);
            if ((chromeDriverExecutable != null) && (chromeDriverExecutable.exists())) {
                log.warn(String.format("Missing property %s but found %s. Using it...",
                        SYSPROP_CHROME_DRIVER_PATH, chromeDriverExecutable.getCanonicalPath()));
                System.setProperty(SYSPROP_CHROME_DRIVER_PATH, chromeDriverExecutable.getCanonicalPath());
            } else {
                log.error(String.format(
                        "Could not find the Chrome driver looking at %s or system path."
                                + " Download it from %s and set its path with " + "the System property %s.",
                        chromeDriverDefaultPath, "http://code.google.com/p/chromedriver/downloads/list",
                        SYSPROP_CHROME_DRIVER_PATH));
            }
        }
    }
    DesiredCapabilities dc = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments(Arrays.asList("--ignore-certificate-errors"));
    Proxy proxy = startProxy();
    if (proxy != null) {
        proxy.setNoProxy("");
        dc.setCapability(CapabilityType.PROXY, proxy);
    }
    dc.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(dc);
}

From source file:org.safs.selenium.webdriver.lib.SelectBrowser.java

License:Open Source License

/**
 * /*from  ww  w. j  ava  2s  .c  o m*/
 * @param browserName String, the browser name, such as "explorer"
 * @param extraParameters Map<String,Object>, can be used to pass more browser parameters, such as proxy settings.
 * @return DesiredCapabilities
 */
public static DesiredCapabilities getDesiredCapabilities(String browserName,
        Map<String, Object> extraParameters) {
    String debugmsg = StringUtils.debugmsg(false);
    DesiredCapabilities caps = null;

    if (browserName.equals(BROWSER_NAME_IE)) {
        System.setProperty(SYSTEM_PROPERTY_WEBDRIVER_IE, "IEDriverServer.exe");
        caps = DesiredCapabilities.internetExplorer();
        caps.setCapability("nativeEvents", true);
        caps.setCapability("requireWindowFocus", true);
        //caps.setCapability("browserName", BROWSER_NAME_IE);
    } else if (browserName.equals(BROWSER_NAME_CHROME)) {
        System.setProperty(SYSTEM_PROPERTY_WEBDRIVER_CHROME, "chromedriver.exe");
        caps = DesiredCapabilities.chrome();

        // Disable extensions to avoid popping up 'Disable developer mode extensions' message by default.
        if (!extraParameters.containsKey(KEY_CHROME_DISABLE_EXTENSIONS)) {
            // Only execute if no user's setting
            extraParameters.put(KEY_CHROME_DISABLE_EXTENSIONS, "true");
        }

        //caps.setCapability("browserName", BROWSER_NAME_CHROME);
    } else if (browserName.equals(BROWSER_NAME_EDGE)) {
        System.setProperty(SYSTEM_PROPERTY_WEBDRIVER_EDGE, "MicrosoftWebDriver.exe");
        caps = DesiredCapabilities.edge();
        //caps.setCapability("browserName", BROWSER_NAME_EDGE);
    } else if (browserName.equals(BROWSER_NAME_ANDROID_CHROME)) {
        System.setProperty(SYSTEM_PROPERTY_WEBDRIVER_CHROME, "chromedriver.exe");
        caps = DesiredCapabilities.chrome();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("androidPackage", "com.android.chrome");
        caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        caps.setCapability(CapabilityType.BROWSER_NAME, BROWSER_NAME_CHROME);
    } else if (browserName.equals(BROWSER_NAME_IPAD_SIMULATOR_SAFARI)) {
        caps = new DesiredCapabilities();
        caps.setCapability("device", "ipad");
        caps.setCapability("simulator", "true");
        caps.setCapability(CapabilityType.BROWSER_NAME, "safari");
    } else { // default browser always
        caps = DesiredCapabilities.firefox();
        caps.setCapability(CapabilityType.BROWSER_NAME, BROWSER_NAME_FIREFOX);
    }

    String unexpectedAlertBehaviour = Processor.getUnexpectedAlertBehaviour();
    if (unexpectedAlertBehaviour == null)
        unexpectedAlertBehaviour = System
                .getProperty(DriverConstant.PROERTY_SAFS_TEST_UNEXPECTEDALERTBEHAVIOUR);
    if (unexpectedAlertBehaviour != null) {
        IndependantLog.debug(debugmsg + " Set '" + unexpectedAlertBehaviour + "' to '"
                + CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR + "'.");
        caps.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, unexpectedAlertBehaviour);
    }

    if (extraParameters != null && !extraParameters.isEmpty()) {
        //1. Add http proxy settings to Capabilities, if they exist 
        Object proxysetting = extraParameters.get(KEY_PROXY_SETTING);
        if (proxysetting != null && proxysetting instanceof String) {
            org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
            proxy.setHttpProxy(proxysetting.toString());

            Object bypass = extraParameters.get(KEY_PROXY_BYPASS_ADDRESS);
            if (bypass != null && bypass instanceof String) {
                proxy.setNoProxy(bypass.toString());
            }

            caps.setCapability(CapabilityType.PROXY, proxy);
        }

        //2 Add firefox profile setting to Capabilities.
        if (BROWSER_NAME_FIREFOX.equals(browserName)) {
            //2.1 Add firefox profile setting to Capabilities, if it exists
            FirefoxProfile firefoxProfile = null;
            Object firefoxProfileParam = extraParameters.get(KEY_FIREFOX_PROFILE);
            if (firefoxProfileParam != null && firefoxProfileParam instanceof String) {
                //Can be profile's name or profile's file name
                String profileNameOrPath = firefoxProfileParam.toString();
                IndependantLog.debug(
                        debugmsg + "Try to Set firefox profile '" + profileNameOrPath + "' to Capabilities.");

                firefoxProfile = getFirefoxProfile(profileNameOrPath);

                if (firefoxProfile != null) {
                    caps.setCapability(KEY_FIREFOX_PROFILE, profileNameOrPath);//used to store in session file
                } else {
                    IndependantLog.error(debugmsg + " Fail to set firefox profile to Capabilities.");
                }
            }
            //2.2 Add firefox profile preferences to Capabilities, if it exists
            Object prefsFileParam = extraParameters.get(KEY_FIREFOX_PROFILE_PREFERENCE);
            if (prefsFileParam != null && prefsFileParam instanceof String) {
                String preferenceFile = prefsFileParam.toString();
                IndependantLog.debug(debugmsg + "Try to Set firefox preference file '" + preferenceFile
                        + "' to Firefox Profile.");
                caps.setCapability(KEY_FIREFOX_PROFILE_PREFERENCE, preferenceFile);//used to store in session file

                Map<?, ?> firefoxPreference = Json.readJSONFileUTF8(preferenceFile);
                if (firefoxProfile == null)
                    firefoxProfile = new FirefoxProfile();
                addFireFoxPreference(firefoxProfile, firefoxPreference);
            }

            if (firefoxProfile != null) {
                caps.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
            }
        }

        //3. Add chrome-options-settings to Capabilities.
        if (BROWSER_NAME_CHROME.equals(browserName) || BROWSER_NAME_ANDROID_CHROME.equals(browserName)) {
            setChromeCapabilities(caps, extraParameters);
        }

        //put extra grid-nodes information
        Object gridnodes = extraParameters.get(KEY_GRID_NODES_SETTING);
        if (gridnodes != null && gridnodes instanceof String) {
            caps.setCapability(KEY_GRID_NODES_SETTING, gridnodes);
        }
    }

    return caps;
}