Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the browser's language in Cypress.io (electron/chrome)?

My question is about configuring Cypress to launch a browser instance in a certain language.

In order to:

  • make assertions on localized (i18n) text labels?
  • check i18n features (switching between languages)
  • bypass issues of Continuous Integration (CI/CD) when, for example, on a local computer, the browser default to fr_FR, and on the CI/CD VM it defaults to en_US?

I tried (without much success):

  • using LANGUAGE=en_US from the terminal invocation,
  • using the Browser's API plugin (see Cypress' browser launch API documentation)

Thanks!

like image 753
David Lacourt Avatar asked Jun 27 '19 13:06

David Lacourt


1 Answers

navigator has two lang props:

  • language ({ value: 'en-GB'}
  • languages(['en-GB'])

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

like image 144
ayxos Avatar answered Sep 23 '22 15:09

ayxos