Change the User Language Locale using Selenium WebDriver

18 April, 2014
Most of the time websites use the user "Default Language Settings" to display the website GUI. If we want to check whether our application is correctly internationalized, then we manually change the language preferences in the browser itself.

However, if we want to check the same using WebDriver then we can programmatically change the user language preferences.

If you are using Firefox then you can change the Language preferences by the below code

intl.accept_languages= "Language Code"

Here is the sample code using TestNG framework.

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 UserLang {

 WebDriver  driver;  
  @BeforeTest  
  public void setUpDriver() {  
  FirefoxProfile profile = new FirefoxProfile();  
  profile.setPreference("intl.accept_languages","de");     
  driver = new FirefoxDriver(profile);  
  }

  @Test  
  public void start(){  
  driver.get("http://google.com");  
  }  
}

When you run the above code then google page will be opened in German Language.




No comments:

Post a Comment