Tuesday, 8 October 2013

How to setup proxy settings for Chrome browser in selenium java

We can set up proxy settings for chrome as below.
Here I have stored proxy url, user name, password in config.properties file and calling to here.. other wise you can give details directly over here as well.

//Proxy settings
 
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
       proxy.setProxyType(ProxyType.MANUAL); 
proxy.setHttpProxy(CONFIG.getProperty("hostname"));
proxy.setSslProxy(CONFIG.getProperty("hostname"));
proxy.setFtpProxy(CONFIG.getProperty("hostname"));
proxy.setSocksUsername(CONFIG.getProperty("username"));
proxy.setSocksPassword(CONFIG.getProperty("password"));
  
   DesiredCapabilities cap = new DesiredCapabilities();
   cap.setCapability(CapabilityType.PROXY, proxy); 
  //DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  //capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user name:password@proxyurl:8080"));
  // WebDriver driver = new ChromeDriver(capabilities);
 
driver = new ChromeDriver(cap);
builder = new Actions(driver); 
bckdbrowser = new WebDriverBackedSelenium(driver,
ConfigReader.ENVIRONMENT_URL);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );

2 comments:

  1. Just in case if you use this one.. you can use this way...
    capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://" + CONFIG.getProperty("username") + ":" + CONFIG.getProperty("password") + "@" + CONFIG.getProperty("hostname")));

    ReplyDelete