在Cypress.io中是否有编程方式来更改用户代理? [英] Is there a programmatic way to change user agent in Cypress.io?
问题描述
我有一些仅在移动设备上进行的广告通话。在Chrome浏览器中,我可以使用设备模式并模拟移动设备,并且来自服务器的最终广告调用已正确地针对移动设备进行了定制。我不确定Chrome如何做到这一点,除非可以发送其他用户代理。
I have some ad calls that are only made on mobile devices. In Chrome, I can use Device Mode and simulate a mobile device, and the resulting ad call from the server is correctly tailored to mobile. I'm not sure how Chrome does this, except possibly by sending a different user agent.
在Cypress.io文档中,它说可以在以下位置更改用户代理。配置文件(Cypress.json)。但是,我需要先对桌面视口进行测试,然后再对具有移动用户代理的移动视口进行测试。有没有办法以编程方式更改用户代理?
In the Cypress.io documentation, it says the user agent can be changed in the configuration file (Cypress.json). But, I need to run a test for a desktop viewport and then a mobile viewport with a mobile user agent. Is there a way to change the user agent programmatically?
推荐答案
聚会晚了一点,但是例如,如果需要,设置 userAgent
作为 Googlebot :
Little late to the party, but if you need, for example, set userAgent
as Googlebot:
before(() => {
cy.visit(url, {
onBeforeLoad: win => {
Object.defineProperty(win.navigator, 'userAgent', {
value: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
});
},
});
});
这篇关于在Cypress.io中是否有编程方式来更改用户代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!