javascript - HTMLUnit: change User Agent string -


i'm using htmlunit in java project in order test web page has got javascript inside. code clicks button calls javascript function, wich redirect user page (like link shortener services). code:

public void click() {     webclient webclient = new webclient();     htmlpage page = webclient.getpage("http://mywebsite.com");     htmlimage = page.gethtmlelementbyid("my_button");     page = (htmlpage) a.click(); } 

the problem htmlunit uses default user agent (internet explorer 8) , there few set (firefox 17 , chrome). behavior of mywebsite.com doesn't change if detects browser/user agent. way, user agent string stored website statistical purposes , need change every time visit it.

i've tried change user agent creating new browserversion object in way:

browserversion bv = new browserversion("chrome", "mozilla/5.0", "mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, gecko) chrome/28.0.1468.0 safari/537.36", 28); 

by way, when instantiate webclient object passing bv object, code doesn't work anymore. i've understood, htmlunit documentation says i've got check if user agent specified in browserversion has proper features run javascript.

however, note constants not enough customize browser, need browserversionfeatures , classes inside "javascript" package.

what mean? why htmlunit doesn't work anymore? goal change user agent string. how can achieve this? note i've tried selenium too, without success. thank help.

edit 1:

found trick. if instantiate browserversion follow:

browserversion bv = new browserversion("netscape", "blablabla", "mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, gecko) chrome/28.0.1468.0 safari/537.36", 0); 

it works, don't understand why. have set first string netscape (tried chrome , mozilla doesn't work). 2nd string random, can put if netscape set first parameter. third string formatted user agent , 4th parameter integer indicating version. can explain me why works if netscape passed first parameter , random others (except 2nd)?

updates:

sometimes doesn't work (as described above). user agents strings, page not correctly loading. cannot understand why user agent should modify behaviour of htmlunit i'm pretty sure javascript it's quite easy , should run browser versions. so, final question is: how can change user agent string in htmlunit without altering behaviour when executing javascript?

it's bit late.. found http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/browserversion.html


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -