'Protractor: change user agent

I'm testing a site designed for mobile pages. It takes screenshots of the page however the screenshots are displaying the screen like on a desktop rather than a mobile device. How can I change the user-agent of chrome (the browser I'm testing with) for these tests. I've tried this with no success:

capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
        args: ['--user-agent ="Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3"'] 
    }
},

The tests run, but this line seems to have no effect.



Solution 1:[1]

You may also want to change the browser size to match the one of your device, for iPhone 5 Retina:

browser.manage().window().setSize(1136, 640);

Place that code within your onPrepare section.

Solution 2:[2]

The configuration syntax in Protractor seems to have changed, for me this worked (iPhone) on the recent Chrome (v38):

capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
        'args': ['user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A405 Safari/600.1.4']
    }
},

Solution 3:[3]

I had a similar situation where I could not get certain chrome arguments to work, like setting user-agent.

The solution for me was to change chromeOptions to goog:chromeOptions in my protractor config.

chromeOptions worked when running via chromedriver on my local machine, but did not work when running on a remote Selenium instance. goog:chromeOptions works on both local and remote.

I am using Protractor version 5.4.2.

Solution 4:[4]

I was facing same issue, couldn't pass iPhone 6 user agent while running protractor tests. Its very simple, just go to your protractor.config.ts and add the below code for chrome capabilities

excludeSwitches: ['enable-automation'],
    args: ['show-fps-counter=true','--window-size=1136, 640'],
    'mobileEmulation': {
      'deviceName': 'iPhone 6'
    },

This works efficiently.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo Gallucci
Solution 2 Jan Petzold
Solution 3 Monkpit
Solution 4 ikhan05