cypress.io设置浏览器语言
具体详细的设置查看stackoverflow:
https://stackoverflow.com/questions/56791796/how-to-set-the-browsers-language-in-cypress-io-electron-chrome
- using the Browser's API plugin (see Cypress' browser launch API documentation)
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', }, });
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作