Test different browser locales in Chrome with Desired Capabilities

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:

  1. Create a New Test Case. Go to File > New > Test Case.

  2. 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.
test case with variables
  1. Use Configured Desired Capabilities. 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.

import com.kms.katalon.core.configuration.RunConfiguration

Map prefs = [('intl.accept_languages') : locale]
// Map preferences key to manipulate page's language.

RunConfiguration.setWebDriverPreferencesProperty("prefs", pref)
  • Continue writing the script or use Web Spy/Record Utility to complete your test case.

Notes:

In case you wish to alter browser locales with an existing test script, copy and paste the above sample code before the test script.

Final results after configuring Desired Capabilities
  • Your Test Case is now ready to run with Chrome in French.

Here is a sample project you can download as a .zip file: Sample test cases run with multiple locales

Create a Test Suite to test different languages

Requirements:

  • Make sure to configure all your test cases with Desired Capabilities as per Part 1.

Notes: Desired Capabilities can be reused across projects. Learn more here: reuse Desired Capabilities

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.

  1. Create a test suite. Go to File > New > Test Suite.
  2. Click Add in the command toolbar, then choose pre-configured test cases.
New Test Suite
  1. 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. You can find language codes for Chrome here: language code.

For our example, we input fr,en,es.

New Data file 2 New Data file
  1. 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.
Show Data Binding section
  • The final results should show as below:
Test Suite data

Here is a sample project you can download as a .zip file: Sample test suite with data binding support.

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.

Requirements:

  • 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. See also update or downgrade WebDrivers.

The following example shows you how to create a custom profile with Spanish as the testing language. Do as follows:

  1. Create a new Custom profile in Desired Capabilities. Go to Project > Settings > Desired Capabilities > Custom.
Project Settings dialog
  1. In the command toolbar, click Add to add a custom profile. In the newly added property line, change the name into "spanish" for better recognition, then click on More (...) under the Value column. A Custom Execution Configuration Builder dialog opens.
Set value for custom Desired Capabilities
  1. 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.
Set value for custom Desired Capabilities
  • Fill in the Driver Builder dialog as shown below:
    1. Remote Server URL: http://localhost:port/wd/hub - the URL to the Remote server.
    2. Remote Server Type: Choose Selenium.
    3. Click Add on the command toolbar as the following command.
    • 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.
Set value for custom Desired Capabilities
  1. Click More (...) under the Preferences column.
  2. The Dictionary Property Builder opens. Input these values:
Set value for custom Desired Capabilities
  1. Click More (...) under the Preferences column.
  2. The Dictionary Property Builder opens. Input these values:
  • intl.accept_languages: Support passing preference key to manipulate a page's language.
  • String: the data type permits you to enter a value directly into the Value cell.
  • es: the language code for Spanish.
Set value for custom Desired Capabilities
  1. The above commands should result in the following:
Results after setting up custom language Remote Control dialog Final Results

Here is a sample project you can download as a .zip file: Sample test cases with custom execution.

Feedback