如何在behat.yml中使用selenium2驱动程序为firefox和chrome指定自定义用户代理

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?

douzhongjian0752
douzhongjian0752 对不起,它不适用于幻影JS和goutte驱动程序。我想将它设置为chrome和firefox。让我重新解释一下我的问题
一年多之前 回复
dshgnt2008
dshgnt2008 可能重复的Mink+PhantomJS:如何设置用户代理?
一年多之前 回复

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"
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐