3

I'd really like to write a Selenium WebDriver test that verifies text is localized. The localization code bases the strings it displays on navigator.language (or navigator.userLanguage for IE). I cannot figure out, however, how to launch a Chromium window for a different language.

I see this bug which sort of explains the problem (closed as not a bug): https://code.google.com/p/chromium/issues/detail?id=114606

Here's how I create the browser instance:

var webdriver = require('selenium-webdriver');
var chrome = require("selenium-webdriver/chrome");

var options = new chrome.Options();
options.addArguments('--lang=es');
var caps = options.toCapabilities();
var driver = webdriver.Builder().withCapabilities(caps).build();

I installed the Spanish language, but whereas navigator.languages includes es in its list, but even if I move it to the top of the preferences, en-US is still what navigator.language is set to.

Have any of you found a way to successfully write multi-language tests with Selenium WebDriver?

  • Ive seen that the --lang=es options usually works. Try to see if you add the Accept-Language header. – LittlePanda May 13 '15 at 16:22
1

This code is working fine for me :

 System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
     ChromeOptions options = new ChromeOptions();
     options.addArguments("--test-type");
     options.addArguments("--lang=hi");
     ChromeDriver wd;
     wd = new ChromeDriver(options);
     wd.manage().window().maximize();
     wd.get("http://google.com");

Its changing the language, showing up as first preferred language.

  • Sorry did'nt notice the programming language. My code is in Java. If it is working in java then it also should work in other as well after changing the syntax. – Juhi Saxena Nov 13 '14 at 13:15
  • Looks like the same thing I tried. – Jacob Nov 13 '14 at 18:51
  • Yes and its working perfectly. – Juhi Saxena Nov 14 '14 at 9:10
  • 2
    It's not working for me. navigator.language is still set to en-US. I'm running this on Linux; not sure if that makes a difference. – Jacob Nov 14 '14 at 15:38
  • @Jacob Does it work when you add that flag manually to the command line? Maybe there's a bug in Chrome for Linux – Yamikuronue Apr 29 '15 at 18:47
0

I resolved the error using this SO post.

The accepted answer from the question:

You can do it by adding Chrome's command line switches "--lang".

Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es, see API for details.

The following is a working example of C# code for how to start Chrome in Spanish using Selenium.

ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);

Java code should be pretty much the same (untested). Remember, locale here is in the form language[-country] where language is the 2 letter code from ISO-639.

public WebDriver getDriver(String locale){   
    System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--lang=" + locale);
    return new ChromeDriver(options);
}

public void initializeSelenium() throws Exception{
    driver = getDriver("es"); // two letters to represent the locale, or two letters + country
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.