I want to execute my Selenium tests in different languages. Is it possible to change the language of an existing WebDriver at runtime or do I have to recreate the browser instance?
Right now I'm only using Firefox, but I want to execute tests in different browsers at some later point.
In Firefox I set the language like this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "de");
WebDriver driver = new FirefoxDriver(profile);
I also implemented a WebDriverPool, which holds a WebDriver instance so it can be shared among tests. If the language can only be set at creation time, I could hold an instance for every locale.
All in all I wonder if I miss something here. Why is it so hard to change the language? shouldn't there be a method like WebDriver.setAcceptLanguages(Locale)
?
In a nutshell I have these questions:
WebDriver.setAcceptLanguages(Locale)
?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.
Selenium WebDriver (Selenium 2.0) is fully implemented and supported in JavaScript (Node. js), Python, Ruby, Java, Kotlin (programming language), and C#.
> Selenium WebDriver supports various programming languages like Java, Python, C#, Ruby, Perl, PHP, JavaScript, R, Objective-C and Haskell. > Java is the best language to start learning Selenium. > Languages that are in demand for automating Selenium are Java, Python, C#, Ruby and JavaScript.
I ended up creating a WebDriverPool that creates one instance for every combination of WebDriver type (e.g. FirefoxDriver.class) and Locale (e.g. en_US). Maybe this is usful for someone.
public class WebDriverPool {
private Map<String, WebDriver> drivers = new HashMap<String, WebDriver>();
private List<WebDriver> driversInUse = new ArrayList<WebDriver>();
public WebDriverPool() {
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
for (WebDriver driver : drivers.values())
driver.close();
if (!driversInUse.isEmpty())
throw new IllegalStateException("There are still drivers in use, did someone forget to return it? (size: " + driversInUse.size() + ")");
}
});
}
private WebDriver createFirefoxDriver(Locale locale){
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", formatLocale(locale));
return new FirefoxDriver(profile);
}
private String formatLocale(Locale locale) {
return locale.getCountry().length() == 0
? locale.getLanguage()
: locale.getLanguage() + "-" + locale.getCountry().toLowerCase();
}
/**
* @param clazz
* @param locale
* @return web driver which can be new or recycled
*/
public synchronized WebDriver getWebDriver(Class<? extends WebDriver> clazz, Locale locale){
String key = clazz.getName() + "-" + locale;
if(!drivers.containsKey(key)){
if(clazz == FirefoxDriver.class){
drivers.put(key, createFirefoxDriver(locale));
}
// TODO create other drivers here ...
// else if(clazz == ChromeDriver.class){
// drivers.put(key, createChromeDriver(locale));
// }
else{
throw new IllegalArgumentException(clazz.getName() + " not supported yet!");
}
}
WebDriver driver = drivers.get(key);
if(driversInUse.contains(driver))
throw new IllegalStateException("This driver is already in use. Did someone forgot to return it?");
driversInUse.add(driver);
return driver;
}
public synchronized void returnWebDriver(WebDriver driver){
driversInUse.remove(driver);
}
}
You can also do it through about:config in firefox. But you need to use Actions to manipulate it.
Below a java piece of code
Actions act = new Actions(webDriver);
webDriver.get("about:config");
// warning screen
act.sendKeys(Keys.RETURN).perform();
// Go directly to the list, don't use the search option, it's not fast enough
act.sendKeys(Keys.TAB).perform();
// Go to the intl.accept_languages option
act.sendKeys("intl.accept_languages").sendKeys(Keys.RETURN).perform();
// fill the alert with your parameters
webDriver.switchTo().alert().sendKeys("fr, fr-fr, en-us, en");
webDriver.switchTo().alert().accept();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With