Configure browser proxy settings using Selenium WebDriver

21 April, 2014
Many times there would be need to test "multilingual" and "multi-regional" websites. If the business is spread across the globe then the website has to offer content in more than one language. The "multi-regional" websites may block users accessing the website from other locations apart from the allowed location. In this case we will have to change the proxy settings of the browser to verify the functionality.
  • A multilingual website is any website that offers content in more than one language (Google).
  • A multi-regional website is one that explicitly targets users in different countries (Bwin).

Say if you directly access the bwin.com from India then we will be redirected to http://help.bwin.com/closed and we will see the below page.


But if you access the same bwin.com site from United States then you will see the below page.


If we want to test this functionality manually then we can get the proxy server details from online and change our browser proxy settings, before accessing the site from our browser.

But if we want to automate the same functionality, then we modify the proxy server settings of the browser from the test script.

Below is the sample program to do this:

import java.io.IOException;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.openqa.selenium.firefox.FirefoxProfile;  
import org.testng.annotations.BeforeTest;  
import org.testng.annotations.Test;  
  
public class ProxySetting {  
   
  WebDriver driver;  
   String serverIP="63.221.140.143";  
    int port=80;     

  @BeforeTest  
    public void setUpDriver() {  
     FirefoxProfile profile = new FirefoxProfile();  
     profile.setPreference("network.proxy.type",1);  
     profile.setPreference("network.proxy.ftp",serverIP);  
     profile.setPreference("network.proxy.http",serverIP);  
     profile.setPreference("network.proxy.socks",serverIP);  
     profile.setPreference("network.proxy.ssl",serverIP);  
     profile.setPreference("network.proxy.ftp_port",port);  
     profile.setPreference("network.proxy.http_port",port);  
     profile.setPreference("network.proxy.socks_port",port);  
     profile.setPreference("network.proxy.ssl_port",port);  
     driver = new FirefoxDriver(profile);  
    }  
     
     
   @Test  
   public void start() throws IOException{  
     driver.get("https://www.bwin.com/");  
     driver.findElement(By.id("username")).sendKeys("myUserName");  
     driver.findElement(By.id("password")).sendKeys("myTestPassword");  
     driver.findElement(By.name("submit")).click();  
   }  
} 

No comments:

Post a Comment