1

My web application renders PC & Smartphone pages depending on the device type used while accessing it.

Browser configuration in my behat.yml is as follows:

.........
Behat\MinkExtension:
  base_url: https://example.com/
  selenium2:
    # ===== configuration for firefox =====
    browser: firefox
    capabilities:
      extra_capabilities:
        trustAllSSLCertificates: true
        acceptSslCerts: true
        proxy:
          proxyType: "manual"
          httpProxy: "proxy_host.com:123"
          sslProxy: "proxy_host.com:123"
........

So how can I specify the custom user agent when behat runs?

Sudheesh.M.S
  • 498
  • 1
  • 7
  • 13
  • Possible duplicate of [Mink + PhantomJS: How do I set the user agent?](https://stackoverflow.com/questions/25319882/mink-phantomjs-how-do-i-set-the-user-agent) – Nico Haase Jan 28 '19 at 11:19
  • Sorry, it's not for phantom JS & goutte driver. I would like to set it for chrome & firefox. Let me rephrase my question – Sudheesh.M.S Jan 28 '19 at 11:40

2 Answers2

1

Specifying user agent while using chrome is pretty straight forward as below:

Behat\MinkExtension:
      base_url: https://example.com/
      selenium2:
        # ===== configuration for chrome =====
        browser: chrome
        capabilities:
          extra_capabilities:
            chromeOptions:
              args:
                - "--user-agent=Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36"
            trustAllSSLCertificates: true
            acceptSslCerts: true
            proxy:
              proxyType: "manual"
              httpProxy: "proxy_host.com:123"
              sslProxy: "proxy_host.com:123"

Whereas, setting user agent in firefox is tricky.

Use the following commands to specify the user agent in zip format in CentOs:

# Creating custom profile for firefox to set mobile user agent
yum install -y zip
mkdir -p /example_path/firefox-profile/
echo 'user_pref("general.useragent.override", "Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36");' >> /example_path/firefox-profile/prefs.js
# prefs.js should be in the root of the archive
zip -rj /example_path/firefox-profile.zip /example_path/firefox-profile/*
rm -rf /example_path/firefox-profile/

And then mention the created firefox profile in behat.yml:

Behat\MinkExtension:
  base_url: https://example.com/
  selenium2:
    # ===== configuration for firefox =====
    browser: firefox
    capabilities:
      firefox:
        # custom profile set for mobile user agent
        profile: /example_path/firefox-profile.zip
      extra_capabilities:
        trustAllSSLCertificates: true
        acceptSslCerts: true
        proxy:
          proxyType: "manual"
          httpProxy: "proxy_host.com:123"
          sslProxy: "proxy_host.com:123"
Sudheesh.M.S
  • 498
  • 1
  • 7
  • 13
0

if you using mink without behat you can do similar things like that :

$mink = new Mink(array(
            'selenium2' => new Session(new Selenium2Driver('chrome', [
                    'browserName'=> 'chrome',
                    'name' => 'serarch bot',
                    '--no-sandbox',
                    '--disable-dev-shm-usage',
                    'chrome' => [
                        'args' => [
                            '--user-agent=Mozilla/5.0 (X11; Linux i686; en-US) Gecko/20071901 Firefox/97.0',
                            '--proxy-server=socks5://5.135.96.34:45786'
                        ]
                    ]

            ], $url)),
        ));
nono
  • 101
  • 4