How to Handle PROXY Settings in Selenium WebDriver.

Hello friends..
Most of the time when we create any webdriver automation code using public websites like Google, Facebook, Yahoo... etc, which works very well at our home pc but not at office.
This may have various reasons. One of the reason is PROXY at your office network.
A proxy or proxy server is basically another computer which serves as a hub through which internet requests are processed. By connecting through one of these servers, your computer sends your requests to the proxy server which then processes your request and returns what you were wanting.

In this way it serves as an intermediary between your home machine and the rest of the computers on the internet. Proxies are used for a number of reasons such as to filter web content, to go around restrictions such as parental blocks, to screen downloads and uploads and to provide anonymity when surfing the internet.
To work your browser under proxy you may need below settings.

Internet Explorer Proxy Settings

  • Click Tools
  • Click Internet Options
  • Click the Connections Tab
  • Click LAN settings
  • Check the “Use a proxy server for your LAN” box
  • Enter the IP Address of the Proxy Server and the Port Number
  • Click OK

FireFox Proxy Settings

  • Click the FireFox Button(The button in the upper left corner)
  • Click Options
  • Click Options in the new tab
  • Click the Advanced Tab
  • Click Settings
  • Click Manual Proxy Settings
  • In the HTTP Proxy Box enter the IP Address of the proxy server and the Port number
  • Click OK

Google Chrome Proxy Settings

  • Click the Customize and Control Button(Button with the wrench picture in upper right corner
  • Click Under the Hood
  • Click Change proxy settings
  • Click LAN Settings
  • Check the “Use a proxy server for your LAN” box
  • Enter the IP Address of the Proxy Server and the Port Number
  • Click OK

Safari Proxy Settings

  • Click Safari
  • Click Preferences
  • Click Advanced
  • Click Change Settings
  • Check the Web Proxy(HTTP) box
  • Enter the IP Address of the Proxy Server and the Port Number
  • Click Apply Now

While launching Browser by Selenium WebDriver these settings are initialized or reset. we need to add some additional code to tell WebDriver about the Proxy setting.
below code snippet is Example of setting proxy server to localhost and port 8080 for InternetExpolrerDriver. This could be apply to any WebDriver.
String PROXY = "localhost:8080";

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);

Many times your Network allow to set Proxy to Auto Detect mode. Below code explains how you can set the WebDriver to Auto Detect Proxy
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setAutodetect(true);

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

Keep Automating..........

No comments: