Reputation: 1165
My question is about configuring Cypress to launch a browser instance in a certain language.
In order to:
fr_FR
, and on the CI/CD VM it defaults to en_US
?I tried (without much success):
LANGUAGE=en_US
from the terminal invocation,Upvotes: 11
Views: 15850
Reputation: 775
In our setup we are using Cypress v12.x with 2 different browsers:
yarn cypress open
)yarn cypress run
)We found that we need to configure both browsers separately
we use the Cypress Browser Launch API (https://docs.cypress.io/api/plugins/browser-launch-api#Changing-browser-preferences) to customize the chrome configuration.
// file cypress.config.js
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on("before:browser:launch", (browser, launchOptions) => {
if (browser.family === "chromium" && browser.name !== "electron") {
launchOptions.preferences.default.intl = {
accept_languages: "en-US,en",
selected_languages: "en-US,en",
};
return launchOptions;
}
});
},
},
});
we use the environment variable ELECTRON_EXTRA_LAUNCH_ARGS
(https://docs.cypress.io/api/plugins/browser-launch-api#Modify-Electron-app-switches) to pass extra args to the electron browser.
// file package.json
"scripts": {
"e2e:run": "ELECTRON_EXTRA_LAUNCH_ARGS=--lang=en yarn cypress run",
...
}
with this configuration the browser is using the specified locale when using the cypress app and also when running the tests on our CI server
Upvotes: 1
Reputation: 1728
Someone got it working with this: (I couldn't)
Source: https://github.com/cypress-io/cypress/issues/7890#issuecomment-824676390
// cypress/plugins/index.js
on('before:browser:launch', (browser, launchOptions) => {
if (browser.name === 'chrome') {
launchOptions.args.push('--lang=en-GB');
return launchOptions;
}
});
Upvotes: -1
Reputation: 41
In support/index.js
Cypress.on('window:before:load', window => {
Object.defineProperty(window.navigator, 'language', { value: 'fr' });
});
Upvotes: 3
Reputation: 1545
To set the language in the browser and also for request, which was what I had to do for my tests, the following worked for me:
cy.visit('url', {
onBeforeLoad(win) {
Object.defineProperty(win.navigator, 'language', { value: 'de-DE' });
Object.defineProperty(win.navigator, 'languages', { value: ['de'] });
Object.defineProperty(win.navigator, 'accept_languages', { value: ['de'] });
},
headers: {
'Accept-Language': 'de',
},
});
Upvotes: 9
Reputation: 408
navigator has two lang props:
navigator.language refers to the first element of navigator.languages but some libraries check navigator.languages[0] instead of navigator.language, so better if you set both properties
onBeforeLoad: (window, ...args) => {
Object.defineProperty(window.navigator, 'language', { value: 'en-GB' });
Object.defineProperty(window.navigator, 'languages', ['en-GB']);
ref: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/languages
Upvotes: 4
Reputation: 951
from Gleb Bahmutov:
you set it during cy.visit using
onBeforeLoad
with something likeObject.defineProperty(navigator, 'language', { value: 'de-GE' })
src: https://gitter.im/cypress-io/cypress?at=5d61408a07d1ff39f8769545
Upvotes: 8