cypress.io设置浏览器语言

具体详细的设置查看stackoverflow:

https://stackoverflow.com/questions/56791796/how-to-set-the-browsers-language-in-cypress-io-electron-chrome

 

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']);

法1:support/index.js,

Cypress.on('window:before:load', window => {
  Object.defineProperty(window.navigator, 'language', { value: 'fr' });
});

法2:

设置在每次访问url之前,前面两句可以对语言设置生效

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

 

posted @ 2022-04-13 20:04  陈晓猛  阅读(249)  评论(0编辑  收藏  举报