My question is about configuring Cypress to launch a browser instance in a certain language.
In order to:
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
In support/index.js
Cypress.on('window:before:load', window => {
Object.defineProperty(window.navigator, 'language', { value: 'fr' });
});
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',
},
});
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