Search code examples
javaseleniumselenium-webdriverselenium-gridselenium-remotedriver

How to set HTTP_PROXY using Selenium RemoteWebDriver?


Here is my current code to launch browser without any proxy:


                properties = getGridProperties();
                DesiredCapabilities capabilities = null;
                FirefoxProfile profile = new FirefoxProfile();
                    profile.setPreference("layout.css.devPixelsPerPx","0.9");
                    FirefoxOptions options = new FirefoxOptions().setProfile(profile);
                    options.addPreference("dom.webnotifications.enabled", false);
                    if (properties.containsKey("hub.gecko.driver.path"))
                        System.setProperty("webdriver.gecko.driver", properties.getProperty("hub.gecko.driver.path"));
                    capabilities = DesiredCapabilities.firefox();
                    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
                    if (browserType.equalsIgnoreCase("oldfirefox")) {
                        capabilities.setCapability("marionette", false);
                        capabilities.setCapability("platform", "LINUX");
                        options.merge(capabilities);
                    }
                printInfo("initRemoteWebDriver:started:" + System.currentTimeMillis());
                capabilities.setCapability("idleTimeout", 150);
                String nodeURL = "http://" + properties.getProperty("grid.hub.host") + "/wd/hub";
                capabilities.setCapability("idleTimeout", 150);
                capabilities.setCapability("name", this.getClass().getCanonicalName());

                driver = new RemoteWebDriver(new URL(nodeURL), capabilities);
                setDriver(driver);
                getDriver().manage().window().maximize();
                printInfo("****Firefox browser launched***");
                printInfo("initRemoteWebDriver:finished:" + System.currentTimeMillis());

Desired proxy details to be set:

HTTP_PROXY = 'http://www-proxy.us.abc.com:80'
HTTPS_PROXY = 'http://www-proxy.us.abc.com:80'

What is the simplest way to do this without changing the current code too much?


Solution

  • Hi please try this one

    Proxy proxy = new Proxy();
    proxy.setHttpProxy("http://www-proxy.us.abc.com:80");
    capabilities.setCapability(CapabilityType.PROXY, proxy);
    

    I hope it will work for you. Thanks