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 in
I am afraid that the whole idea of WebDriver is to act like browser - so you can change the language of the browser, but you have to change the locale in the Operating system, or hope that the application will do it for you.
For instance - German number format separates decimal number by comma and English one by dot. If you want to test, how the number format behaves in English locale and in German locale, you can do it only by these two approaches:
To answer your questions:
I would do it like this (Java code):
private WebDriver driver;
public enum Language {en-us, de}
public WebDriver getDriver(Language lang){
String locale = lang.toString();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", locale);
driver = new FirefoxDriver(profile);
return driver;
}
@Test
public void TestNumber(){
WebDriver drv = getDriver(Language.en);
drv.get("http://the-site.com");
WebElement el = drv.findElement //... find element
String number = el.getText();
Assert.assertEquals(number, "123.45");
drv.close();
drv = getDriver(Language.de);
drv.get("http://the-site.com");
WebElement el = drv.findElement //... find element
String number = el.getText();
Assert.assertEquals(number, "123,45");
drv.close();
}
I am afraid you have to close the browser and open it again with different language.
BTW the above bit of code assumes, that the web application changes the way how to show numbers to the user based on browser language.