简体   繁体   中英

Cannot change language of selenium-webdriver for firefox

I am running e2e tests based on selenium-webdriver (firefox). Some of my test cases are based on comparing result messages. The problem is when I run tests on my local machine (and my local firefox browser) the language is automatically changed from EN, which ruin some of my test cases (which compare same messages but in different languages). Firstly I tried changing it in my browser. I reorder my preferred languages list, so that EN is on the first position. On every test run it all goes back to the original order (with EN on the second place). I also tried removing the language that is being chosen automatically. But after first tests run, the list of languages gets back to it previous form (with EN being second).

Next, I tried setting language for my webdriver. My final code:

let profile = new firefox.Profile().setPreference("intl.accept_languages", "en-US");
let options = new firefox.Options().setProfile(profile);

let driverBuilder = new selenium.Builder().forBrowser(webDriverConfig.browserName).setFirefoxOptions(options).build();

But this take no effect at all. During tests I still get wrong locale and after running tests in my firefox settings I am still getting wrong language at first place (in language preference list). How can I set english to be my accepted/preferred language during my tests?

OK, I have resolved it. The problem was I mixed 2 approaches, both of them I found in comments in selenium-webdriver code. First one that I found on github (most recent master branch) looks like that:

let options = new firefox.Options()
    .addExtensions('/path/to/firebug.xpi')
    .setPreference('extensions.firebug.showChromeErrors', true);

So, here it is available to call setPreference() on Options object. This code snippet comes from most recent master branch. The problem is, the current master is released as unstable (4.0.0-alpha.sth...). The last stable version that was released is 3.6.0 and there are many differences between apis. So I found similar code example directly in the selenium-webdriver package that I am referencing in my project, and setting preferences in that version looks like that:

let profile = new firefox.Profile();
profile.addExtension('/path/to/firebug.xpi');
profile.setPreference('extensions.firebug.showChromeErrors', true);

let options = new firefox.Options().setProfile(profile);

In this, stable version of api, setPreference() method belongs to the Profile class. But apart from that there is another big difference. In 4.0.0 version api, the method returns modified object itself whereas in the stable version, the method only sets the state. So this doesn't work in the stable version:

let profile = new firefox.Profile().setPreference("intl.accept_languages", "en-US");

And this is the correct code:

let profile = new firefox.Profile();
profile.setPreference("intl.accept_languages", "en,en-US");

Finally, if you want to set locale for firefox webdriver, here are working examples of code for selenium-webdriver 3.6.0:

let profile = new firefox.Profile();
profile.setPreference("intl.accept_languages", "en,en-US");

let options = new firefox.Options().setProfile(profile);

let driver = new selenium.Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(options)
    .build();

And for selenium-webdriver 4.0.0:

let options = new firefox.Options()
    .setPreference("intl.accept_languages", "en,en-US");

let driver = new selenium.Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(options)
    .build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM