I am trying to use a custom HTTP UserAgent Header string to bypass a captcha code on our website. It works correctly manually, and I am trying to make it work with my automation tests. I write them in PHP using the Codeception framework. As you can see below, I have tried adding variables to my config yml for browserstack-sierra-safari. "headers" "User-Agent". I've tried adding this code to win7-chrome, as well as up in the env. I have also tried the variable browserstack.useragent and browserstack.user-agent. My config file is below.

class_name: AcceptanceTester

    modules:
      enabled:
        - WebDriver
        - Helper\Acceptance
        - Helper\CaptchaSolver
        - Asserts

      config:
        WebDriver:
          url: '**********************'
          browser: chrome



    env:
      prod:
          modules:
            config:
               WebDriver:
                  url: '**********************'
      test:
          modules:
            config:
               WebDriver:
                  url: '************************'

      dev:
          modules:
            config:
               WebDriver:
                  url: '********************'
      browserstack-win7-chrome:
            modules:
              config:
                 WebDriver:
                    host: '**************************'
                    port: 80
                    browser: chrome
                    capabilities:
                     browserstack.user: 'a******'
                     browserstack.key: '******************'
                     browserstack.console: 'verbose'
                     browserstack.idleTimeout: 300
                     acceptSslCerts: true
                     os: Windows
                     os_version: 7
                     browserstack.local: true
                     browserstack.debug: true

      browserstack-sierra-safari:
          modules:
            config:
               WebDriver:
                  host: '******************@hub.browserstack.com'
                  port: 80
                  browser: edge
                  capabilities:
                   os: Windows
                   os_version: 7
                   browserstack.local: true
                   browserstack.debug: true
                   browserstack.acceptSslCerts: true


        headers:

 Accept:'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
            Accept-Language: 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'
            User-Agent: 'Mozilla/5.0 (Windows NT 10.0; WOW64)' 

AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
      browserstack-win10-edge:
            modules:
              config:
                 WebDriver:
                    host: '****************@hub.browserstack.com'
                    port: 80
                    browser: Edge
                    capabilities:
                     os: Windows
                     os_version: 10
                     browserstack.local: true
                     browserstack.debug: true

Has anyone successfully sent an HTTP header through browserstack in an automation test? If so, what variable was used?

1

There are 1 best solutions below

0
On

Ideally, I would recommend you override the useragent while instantiating the browser.

For eg, if you are using chrome and tests are implemented in java, below code should help:

ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent=Mozilla/5.0 (Linux; Android 6.0; HTC One M9 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36");
driver = new ChromeDriver(options);

on similar lines, while implementing it on browserstack, you may use the below lines of code for your webdriver call

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent=Mozilla/5.0 (Linux; Android 6.0; HTC One M9 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new RemoteWebDriver(new URL(“https://" + <BROWSERSTACK_USERNAME> + ":" + <BROWSERSTACK_KEY> + "@hub-cloud.browserstack.com/wd/hub”), capabilities);

Not sure if Browserstack has any capability called browserstack.useragent or browserstack.user-agent