Chrome sets a default UI language with the first Chrome window that opens. In other words, if you alter browser locales, for example, with command line argument
chrome.exe--lang=de to start Chrome in German, the Chrome driver still defines the default language from the Chrome browser.
To test different browser locales, you can instead configure Desired Capabilities. You can learn about this here: Desired Capabilities
Use Configured Desired Capability with Test Case Variables
In this section, we show you two possible approaches to alter browser locales while testing:
- To test one specific language with a test case.
- To test different languages with a test suite.
Create a test case to test one language
In the following example, we configure a test case with a specific browser locale, like French.
Do as follows:
Create a New Test Case. Go to File > New > Test Case.
Create Test Case Variables. See also: Test Case Variables.
- Switch to the Variables tab of your Test Case.
- Click Add. A new row appears in the variable list.
- Input the "locale" variable like so:
Name Type Default Value locale String "fr"
In our example, the Default Value is
fr, the language code for French. You can find other language codes for Chrome here: language code.
After defining Test Case Variables, we override default language settings in Chrome by using Configured Desired Capabilities. You can learn more about this here: Configured Desired Capabilities
- Switch to the Script tab of your Test Case.
Copy and paste the below code into your test script. With this code, you can manipulate the locales of the testing browsers.
Map prefs = [('intl.accept_languages') : locale]
// Map preferences key to manipulate page's language.
Continue writing the script or use Web Spy/Record Utility to complete your test case.Note:
In case you wish to alter browser locales with an existing test script, copy and paste the above sample code before the test script.
Your Test Case is now ready to run with Chrome in French.
Create a Test Suite to test different languages
- Make sure to configure all your test cases with Desired Capabilities as per Part 1.
Desired Capabilities can be reused across projects. You can refer to this document: Reuse Desired Capabilities for further details.
In the following example, we demonstrate how to create a Test Suite with Test case variables to test different browser locales. Here, we use French, English, and Spanish.
- Create a test suite. Go to File > New > Test Suite.
Click Add in the command toolbar, then choose pre-configured test cases.
Create a data file. Go to File > New > Test Data. Choose Data Type as Internal Data.
You use this data file to input different language codes you want to test on browsers. For our example, we input
Manage Data Binding
Return to your test suite, click Show Data Binding to expand the Data Binding section. Make sure you click on the correct pre-configured test case beforehand.
This step binds the New Data File from Step 3 with the Test Suite you want to run. See also Manage Data Binding.
The final results should show as below:
Use Custom Profiles in Desired Capabilities
You can also test different browser locales with a Remote Server. In this case, you can set Custom Desired Capabilities to alter the default language in Chrome.
Here is a sample project you can download as a .zip file: Sample test cases with custom execution.
- Make sure that you are running Selenium Grid Hub & Node while executing the test.
- Make sure to update the browser by clicking Tools > Update WebDrivers > Choose browser.
The following example shows you how to create a custom profile with Spanish as the testing language. Do as follows:
Create a new custom profile in Desired Capabilities. Go to Project > Settings > Desired Capabilities > Custom.
In the command toolbar, click Add to add a custom profile. In the newly added property line, change the name to
spanishfor better recognition, then click on More (...) under the Value column. A Custom Execution Configuration Builder dialog opens.
In the Custom Execution Configuration Builder dialog, specify the Driver Name as Remote, then click on More (...) under the Preferences column. A Driver Builder dialog opens.
Fill in the Driver Builder dialog as shown below:
- Remote Server URL:
http://localhost:port/wd/hub- the URL to the Remote server.
- Remote Server Type: Choose Selenium.
Click Add on the command toolbar, then input the following values:
Table 1 Name Type Value browserName string chrome goog:chromeOptions(*) Dictionary(**) Click More (...). In the pop-up Dictionary Property Builder dialog, click Add, then input values from Table 2.
goog:chromeOptions: Support passing the ChromeOptions object into the ChromeDriver constructor.
Dictionary: the data type permits you to input a collection of keys and values. You can learn more about this here: Data types.
Table 2 Name Type Value prefs Dictionary Click More (...). In the pop-up Dictionary Property Builder dialog, click Add, then input values from Table 3. Table 3 Name Type Value intl.accept_languages(*) String es(**)
intl.accept_languages: Support passing preference key to manipulate a page's language.
es: the language code for Spanish.Note:
- The capabilities properties are case-sensitive.
- Remote Server URL:
- Click OK to save the settings in each table. The above commands should result in the following: