UserAgent on htmlunit webdriver

3,059 views
Skip to first unread message

jayesh

unread,
Aug 24, 2011, 12:21:15 AM8/24/11
to Selenium Users
Hi,

I have been trying to change the useragent on htmlunit driver ,I have
not been able to figure a way to implement the same.If someone has
done that already , can you please help me ..

Jayesh

PeterJef...@hotmail.co.uk

unread,
Aug 25, 2011, 5:50:30 AM8/25/11
to Selenium Users
Not somethign I've had to do myself yet, but I can see that I might
need to soon.

Looking at this page on the HTML unit sourcefore:

http://htmlunit.sourceforge.net/gettingStarted.html

. . . I understand that you'd need to change the browser version that
html uit is masquerading as, which implicitly changes the user agent,
so something like this should work:

DesiredCapabilities capabilities =
DesiredCapabilities.htmlUnit();
capabilities.setCapability(CapabilityType.BROWSER_NAME,
BrowserVersion.INTERNET_EXPLORER_7);
driver = new HtmlUnitDriver(capabilities);

Peter

Mark Collin

unread,
Aug 25, 2011, 5:56:10 AM8/25/11
to seleniu...@googlegroups.com
My current implementation is as follows:

public enum htmlUnitEmulation {

NONE, FIREFOX, IE6, IE7, IE8
}

private DesiredCapabilities setHTMLUnitCapabilities(htmlUnitEmulation
browserVersion) {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setJavascriptEnabled(true);
switch (browserVersion) {
case FIREFOX:
capabilities.setBrowserName("firefox");
break;
case IE6:
capabilities.setBrowserName("internet explorer");
capabilities.setVersion("6");
break;
case IE7:
capabilities.setBrowserName("internet explorer");
capabilities.setVersion("7");
break;
case IE8:
capabilities.setBrowserName("internet explorer");
capabilities.setVersion("8");
break;
default:
break;
}
return capabilities;
}

driverObject = new
HtmlUnitDriver(setHTMLUnitCapabilities(htmlUnitEmulation.NONE));

This should be expandable in the future. I also use a properties file to
set the type of emulation, the value in the properties file is matched to
one of the values in the enum (or none if it can't be matched).

http://htmlunit.sourceforge.net/gettingStarted.html

Peter

--
You received this message because you are subscribed to the Google Groups
"Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to
selenium-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

jayesh murali

unread,
Aug 25, 2011, 11:55:04 AM8/25/11
to seleniu...@googlegroups.com
Hi Peter,Mark,

Thanks for the reply .Please correct ,me if I am worng , I think these are solution to emulate a browser using HTML unit .But What I would like to do is set User agents for these emulated borwsers .Will the below solution help me achieve it ?

Regards
Jayesh

jayesh murali

unread,
Aug 25, 2011, 12:11:41 PM8/25/11
to seleniu...@googlegroups.com
After going through the solutions from Peter and Mark , I think this might work .
 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
 capabilities.setCapability("UseragentName", "UserAgenetvalue");
 HtmlUnitDriver driverHTML = new HtmlUnitDriver(capabilities);

Regards
Jayesh

Mark Collin

unread,
Aug 25, 2011, 12:11:52 PM8/25/11
to seleniu...@googlegroups.com

Jayesh,

 

I’ve obviously misunderstood your requirement, the code I supplied is setting HTMLUnit to simulate the browsers, not modify the user agent string (Although it will probably implicitly do that, but you are stuck with a limited selection that are currently supported by Selenium’s capabilities code).

PeterJef...@hotmail.co.uk

unread,
Aug 25, 2011, 1:02:01 PM8/25/11
to Selenium Users
Jayesh

Selenium WebDriver simply wraps the HTML unit api, so you're limited
to what HTML unit offers and as well as what Selemium offers, as Marks
says.

The getting started page on HTML unit I referred you to makes it clear
that you cannot change the user agent except by changing the browser
that it emulates, and Selenium won't be able to do any more than the
underlying html unit api allows. Your code seems like a stab in the
dark so there seems no reason to expect it to work, though by all
means try it and see.

As an alternative approach, you can certainly set manually change the
user agent string in the configuration options of Firefox (can't
remember where just now) and seemingly also in Internet explorer
through the group policies editor. I think the Firefox driver lets to
you set more of its properties from code, if you need to switch it in
the middle of a test run so using Firefox might be your best option.

Is there any reason you need to specifically use html unit rather than
one of the 'real 'browser drivers, other than for increased speed? If
not,you might have to make a compromise there.

Peter

On Aug 25, 5:11 pm, jayesh murali <jayesh.muralidha...@gmail.com>
wrote:
> After going through the solutions from Peter and Mark , I think this might
> work .
>  DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
>  capabilities.setCapability("UseragentName", "UserAgenetvalue");
>  HtmlUnitDriver driverHTML = new HtmlUnitDriver(capabilities);
>
> Regards
> Jayesh
>
> On Thu, Aug 25, 2011 at 9:55 AM, jayesh murali <
>
>
>
> jayesh.muralidha...@gmail.com> wrote:
> > Hi Peter,Mark,
>
> > Thanks for the reply .Please correct ,me if I am worng , I think these are
> > solution to emulate a browser using HTML unit .But What I would like to do
> > is set User agents for these emulated borwsers .Will the below solution help
> > me achieve it ?
>
> > Regards
> > Jayesh
>
> >> postmas...@ardescosolutions.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Selenium Users" group.
> >> To post to this group, send email to seleniu...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> selenium-user...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/selenium-users?hl=en.- Hide quoted text -
>
> - Show quoted text -

Luke Inman-Semerau

unread,
Aug 25, 2011, 1:09:42 PM8/25/11
to seleniu...@googlegroups.com
If you're in java land you can Extend the HTMLUnitDriver class and make a public getWebClient() method... then set it to your hearts content (to change to say iOS user agents etc...)

jayesh murali

unread,
Aug 25, 2011, 1:19:09 PM8/25/11
to seleniu...@googlegroups.com
Peter,

I was thinking of switching to HTML unit driver  from Firefox driver  to check if we can have a better performance   .But Looks we will have to make a compromise here just as you have hinted.

Thanks
Jayesh

jayesh murali

unread,
Aug 25, 2011, 1:38:41 PM8/25/11
to seleniu...@googlegroups.com
Thanks .It works .

Regards
Jayesh

Mark Collin

unread,
Aug 25, 2011, 1:53:22 PM8/25/11
to seleniu...@googlegroups.com
The next question is why are you setting the user agent anyway? Does the website you are testing display differently depending upon the user agent it detects?

jayesh murali

unread,
Aug 25, 2011, 2:00:20 PM8/25/11
to seleniu...@googlegroups.com
Yes .User Agent for Mobile .

Regards
Jayesh

Luke Inman-Semerau

unread,
Aug 25, 2011, 4:02:14 PM8/25/11
to seleniu...@googlegroups.com
Have you looked into the AndroidDriver or iPhoneDriver?

jayesh murali

unread,
Aug 25, 2011, 8:16:20 PM8/25/11
to seleniu...@googlegroups.com
Yes. I am aware of it .This is just an interim solution.

Regards
Jayesh

kavi

unread,
May 29, 2013, 1:23:43 PM5/29/13
to seleniu...@googlegroups.com

Hi There, 

Could you get HTMLUNITdriver able to set user agent? I'm not able to set using capailites

capabilities.setCapability("UseragentName", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13");

//capabilities.

capabilities.setBrowserName("Mozilla/5.0");

capabilities.setPlatform(Platform.MAC);

capabilities.setVersion("Chrome/24.0.1290.1 Safari/537.13");


Lukas Mega

unread,
Dec 2, 2013, 8:23:07 AM12/2/13
to seleniu...@googlegroups.com
Hi guys,

I found solution, how set custom User Agent string for HtmlUnitDriver:

final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20160101 Firefox/66.0";

driver = new HtmlUnitDriver(new BrowserVersion(
                "Firefox", "5.0 (Windows)", USER_AGENT, 28
));



It works. I tested it on http://myhttp.info for getting user agent from remote server.

    @Test
    public void testUserAgent() throws Exception {
        driver.get("http://myhttp.info");
        MyHttpInfoPage myHttpInfoPage = PageFactoryExtended.initElements(driver, MyHttpInfoPage.class);
        String userAgent = myHttpInfoPage.getUserAgent();
// @FindBy(xpath = "//td[text()='User agent']/following-sibling::td")
        Assert.assertEquals(userAgent, USER_AGENT);
    }


Regards
Lukáš M.
Reply all
Reply to author
Forward
0 new messages