Switching/faking user agent for WebDriver anytime after instantiation?

85 views
Skip to first unread message

David

unread,
Feb 26, 2015, 5:22:10 PM2/26/15
to seleniu...@googlegroups.com
I know one can define the user agent for some browsers via profile configuration or desired capability settings when instantiating the driver for browsers like FF, Chrome.

I was wondering though, whether it might be possible to switch original user agent setting defined at driver instance creation at any point after that during test run. Something like what the user agent switching browser extensions offer you for manual testing (where you swap user agent via the extension and the browser auto refreshes the current page to mimic the updated user agent string). Is that kind of behavior possible with WebDriver for say Firefox, and/or Chrome? Or is it a limitation that you can only set user agent at driver instance creation?

PeterJeffreyGale

unread,
Feb 26, 2015, 5:50:32 PM2/26/15
to seleniu...@googlegroups.com
Should work. Try it and see.

krishnan.ma...@gmail.com

unread,
Feb 26, 2015, 9:19:27 PM2/26/15
to seleniu...@googlegroups.com
David 

webdriver by itself is not doing anything here. The Firefox profile way of doing it is because Firefox has the concept of profiles and webdriver just leverages it. 

Altering the user agent or any such thing should ideally be done by leveraging a proxy server such as BrowserMob Proxy. 

That way the implementation would work on all browsers because all webdriver implementations let you use a proxy server. 


- Krishnan

This message may include free added typos, which you may use for your entertainment.
Courtesy : my SmartPhone!



On Thu, Feb 26, 2015 at 2:22 PM -0800, "David" <mang...@gmail.com> wrote:

I know one can define the user agent for some browsers via profile configuration or desired capability settings when instantiating the driver for browsers like FF, Chrome.

I was wondering though, whether it might be possible to switch original user agent setting defined at driver instance creation at any point after that during test run. Something like what the user agent switching browser extensions offer you for manual testing (where you swap user agent via the extension and the browser auto refreshes the current page to mimic the updated user agent string). Is that kind of behavior possible with WebDriver for say Firefox, and/or Chrome? Or is it a limitation that you can only set user agent at driver instance creation?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/70be4c42-f4d6-4ca3-b011-94b82dbff623%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David

unread,
Feb 27, 2015, 2:08:51 PM2/27/15
to seleniu...@googlegroups.com
Thanks for the tip Krishnan. I hadn't considered that.

Regarding proxy, I assume that solution might be harder to configure if the proxy is shared across nodes and tests (potentially) running in parallel? Say for example, for some specific tests I want to dynamically change user agent at various points of the test, but for all other tests I want to use the browser's default user agent. That shouldn't be a problem if the rest of the tests aren't set to use a proxy. But say I wanted to use proxy for them for other purpsoses. I guess could perhaps have 2 separate proxy servers for handling different use cases. This is simplest for a standalone environment where proxy and tests can be on local machine and run 1-to-1 w/ no sharing of resources. Potentially more complicated when using with Grid and parallel tests depending on how the system is configured.

Krishnan Mahadevan

unread,
Feb 27, 2015, 3:10:49 PM2/27/15
to Selenium Users
David,
I believe now you should be able to configure a proxy server instance on a per WebDriver instance. IE was the only bottle neck until sometime back. But thanks to Jim Evans, now IE also can be configured to use a proxy server per IEDriverServer instance. The same holds true with ChromeDriver as well.

With respect to adding user agents etc., I think that now becomes part of the Proxy Server. For e.g., in BrowserMob Proxy you could add request interceptors and within the interceptor you can enable/disable different aspects of a HttpRequest at will. So pretty much a lot of things should be possible with Proxy Servers such as BMP. The Grid environment is no exception as well.

I must admit I havent given BMP a try off late. I understand that the latest version of BMP has a lot of fixes. So its worth giving it a shot and see if it helps with what you are trying to get done.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Reply all
Reply to author
Forward
0 new messages