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

后端 未结 4 1301
感动是毒
感动是毒 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:14

    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',
        },
    });
    

提交回复
热议问题