Is there a programmatic way to change user agent in Cypress.io?

前端 未结 4 1822
盖世英雄少女心
盖世英雄少女心 2021-01-04 10:34

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 corre

4条回答
  •  感情败类
    2021-01-04 11:13

    Update: According to https://github.com/cypress-io/cypress/issues/3873 it is possible since Cypress 3.3.0 use user-agent property in a cy.request() and cy.visit().

    If you need, for example, set userAgent as Googlebot:

    cy.visit(url, {
        headers: {
            'user-agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        }
    });
    

    Original answer before Cypress 3.3.0

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

提交回复
热议问题