Setting browser User Agent with Browserstack

1,567 views
Skip to first unread message

Shane Courville

unread,
Apr 26, 2017, 2:27:03 PM4/26/17
to Geb Development Mailing List
Hi all,

Not sure if this is the correct forum for this, but I am currently trying to figure out a way to set a browser's user-agent through Browserstack.

Currently, this is what our browser config area looks like in build.gradle:

browsers {
chrome_windows {
capabilities browser_version: "56.0", os: "Windows", os_version: "10", context: "desktop"
}

edge_windows {
capabilities browser_version: "14.0", os: "Windows", os_version: "10", context: "desktop"
}

ie_windows {
capabilities browser_version: "11.0", os: "Windows", os_version: "10", context: "desktop"
}

ipad2 {
capabilities browserName: "iPad", platform: "MAC", device: "iPad Air 2", context: "tablet"
}

iphone6S {
capabilities browserName: "iPhone", platform: "MAC", device: "iPhone 6S", context: "mobile"
}
}

We were thinking of trying to add this at the end of the capabilities, but it did not work:


user_agent: "--user-agent=<myagent>"

Marcin Erdmann

unread,
Apr 27, 2017, 3:58:48 AM4/27/17
to geb...@googlegroups.com
Hi Shane,

The user group (https://groups.google.com/d/forum/geb-user) would probably be a better forum for this question as this is a list for discussions about development of Geb itself. I think we can live with this question here but please keep that in mind for the future.

Setting a user agent is browser specific (as this SO answer explains: http://stackoverflow.com/a/29966769) so you won't get a one solution for all browsers to this problem.

In general you would need to create a custom implementation of BrowserStackDriverFactory which overrides configureCapabilities(DesiredCapabilities) and use that in your GebConfig.groovy instead of the one Geb ships with. It would look like the following if you were to do it for Firefox as an example:

class CustomUserAgentBrowserStackDriverFactory extends BrowserStackDriverFactory {

    protected void configureCapabilities(DesiredCapabilities desiredCapabilities) {
        super.configureCapabilities(desiredCapabilities)
        if (desiredCapabilities.getCapability("browserName") == "firefox") {
            def profile = new FirefoxProfile()
            profile.setPreference("general.useragent.override", "whatever you want")
            desiredCapabilities.setCapability(FirefoxDriver.PROFILE, profile)
        }
    }

}

So basically you would be using the fact that overriding configureCapabilities(DesiredCapabilities) allows you to modify the DesiredCapabilities before a browser is provisioned for you by BrowserStack. You might find the following link useful as well as it explains how to configure ChromeOptions which are aparently used to set user agent in chrome: https://www.browserstack.com/automate/java#chrome-options.

Please let me know if you need more help with that.

Cheers,
Marcin

--
You received this message because you are subscribed to the Google Groups "Geb Development Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-dev+unsubscribe@googlegroups.com.
To post to this group, send email to geb...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-dev/b771ca8f-0118-4831-9234-954d521bed9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shane Courville

unread,
Apr 27, 2017, 9:40:06 AM4/27/17
to Geb Development Mailing List
Hi Marcin,

Thank you for the response. This helps! I'll use the other user group in the future.

Shane
To unsubscribe from this group and stop receiving emails from it, send an email to geb-dev+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages