duanchongchu5177 2019-01-28 11:16 采纳率: 0%
浏览 84

如何在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?

  • 写回答

1条回答 默认 最新

  • douchen7324 2019-01-28 11:36
    关注

    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"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?