Dynamically changing proxy in browsers with Selenium webdriver



Dynamically changing proxy in different browsers with Selenium webdriver

how to change the proxy in selenium


The earliest and recommended way to change the proxy settings is to change the proxy manually from the browser settings. But in the automation perspective manual intervention is not a good approach.  So what we need to do is to change the proxy settings in runtime. Now, the question is why is it required to change the proxy. The answer is simple. While trying to access some secure application a proxy is needed because otherwise it is impossible to access that very application. Or, while working on a secure environment where every traffic must go through a specific proxy, providing a separate proxy is absolutely necessary to access the application.

Setting up the proxy manually from a web browser



setting up proxy using selenium
In the above image it can be seen that there is a section for the proxy server.  Now the required process is to set the proxy server in Address field and the port number in the port field.
How to change the proxy settings in runtime.

There are several ways to change the proxy settings in runtime and creating object of proxy class is one of them.

Chrome and Internet explorer share the same proxy settings, so same line of code will work for both of the browsers. But when it comes to Firefox there are some changes in that line of code. The Firefox Version 48 and the newer versions require GeckoDriver and a different setup for adding proxy. As  per seleniumhq
“Firefox maintains its proxy configuration in a profile. You can preset the proxy in a profile and use that Firefox Profile or you can set it on profile that is created on the fly as is shown in the following example. With GeckoDriver the proxy has to be passed through the required capabilities.”



Proxy setup for IE,Chrome and Firefox(upto 47)
 String PROXY = "abc.xyz.com:8060";  
 org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();  
    proxy.setHttpProxy(PROXY)  
      .setFtpProxy(PROXY)  
      .setSslProxy(PROXY);  
 DesiredCapabilities cap = new DesiredCapabilities();  
 cap.setCapability(CapabilityType.PROXY, proxy);  

Proxy setup for Firefox(version 48+)

 String PROXY = "localhost";  
 int PORT = 8080;  
 com.google.gson.JsonObject json = new com.google.gson.JsonObject();  
 json.addProperty("proxyType", "MANUAL");  
 json.addProperty("httpProxy", PROXY);  
 json.addProperty("httpProxyPort", PORT);  
 json.addProperty("sslProxy", PROXY);  
 json.addProperty("sslProxyPort", PORT);  
 DesiredCapabilities cap = new DesiredCapabilities();  
 cap.setCapability("proxy", json);  
 GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)  
  .usingDriverExecutable(new File("path to geckodriver"))  
  .usingAnyFreePort()  
  .usingAnyFreePort()  
  .build();  
 service.start();  
 // GeckoDriver currently needs the Proxy set in RequiredCapabilities  
 driver = new FirefoxDriver(service, cap, cap);  

There is the complete code for chrome browser

 package com.test;  
 import java.io.File;  
 import java.util.concurrent.TimeUnit;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.remote.CapabilityType;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
 public class FullPage_Screenshot {  
   public static void main(String[] args) throws Exception {  
    String PROXY = "abc.xyz.com:8060";  
    org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();  
    proxy.setHttpProxy(PROXY)  
      .setFtpProxy(PROXY)  
      .setSslProxy(PROXY);  
    DesiredCapabilities cap = new DesiredCapabilities();  
    cap.setCapability(CapabilityType.PROXY, proxy);  
    //WebDriver driver = new InternetExplorerDriver(cap);  
    System.setProperty("webdriver.chrome.driver", "D:/workspace/TestProject/src/chromedriver.exe");   
  WebDriver driver = new ChromeDriver(cap) ;  
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);  
     driver.get("https://www.google.com");  
     driver.manage().window().maximize();    
     driver.quit();  
   }   
 }  

Share this

Related Posts

Previous
Next Post »

1 comments:

comments