Q:

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

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.
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?  

A:

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

A:

Now cypress supports passing user agent in the header for cy.visit as well as cy.request:
it('Verify Social Sharing Meta Tags', () => {
  cy.visit(portalURL + '/whats_new/140', {
    headers: {
      'user-agent': 'LinkedInBot/1.0 (compatible; Mozilla/5.0; Apache-HttpClient +http://www.linkedin.com)',
    }
  });

  cy.document().get('head meta[name="og:type"]')
    .should('have.attr', 'content', 'website');
});

https://on.cypress.io/changelog#3-3-0

