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

后端 未结 4 1289
感动是毒
感动是毒 2021-02-14 10:00

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?<
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 10:13

    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

提交回复
热议问题