How to set browser default language in Cypress

If running cypress in headless mode, the browser default language might not be set correctly, which causes assertions for translation values to fail.

The first of the two possible solutions is setting the default languageĀ as the second argument of the cy.visit command in the “onBeforeLoad” callback:

cy.visit('/my-page',{
  onBeforeLoad (win) {
    Object.defineProperty(win.navigator, 'language', {
      value: 'de'
    })
  }
});

The second and to my opinion better solution is to set in globally in the plugins file:

// cypress/plugins/index.js
module.exports = (on, config) => {
  on('before:browser:launch', (browser = {}, launchOptions) => {
    if (browser.name === 'chrome') {
      launchOptions.args.push('--lang=de');
      return launchOptions;
    }
  });
};