title: HTMLUnit: change User Agent string
body: <p>I'm using HtmlUnit in my Java project in order to test a web page that has got Javascript inside. My code clicks a button that calls a Javascript function, wich redirect the user to another page (like link shortener services). This is the code:</p>
  
  <pre><code>public void click()
  {
      WebClient webClient = new WebClient();
      HtmlPage page = webClient.getPage("http://mywebsite.com");
      HtmlImage a = page.getHtmlElementById("my_button");
      page = (HtmlPage) a.click();
  }
  </code></pre>
  
  <p>The problem is that HTMLUnit uses the default User Agent (Internet Explorer 8) and there are a only few to set (Firefox 17 and Chrome). The behavior of mywebsite.com doesn't change if it detects another browser/user agent. By the way, the user agent string is stored by the website for statistical purposes and I need to change it every time I visit it.</p>
  
  <p>I've tried to change the User Agent by creating a new <code>BrowserVersion</code> object in this way:</p>
  
  <pre><code>BrowserVersion bv = new BrowserVersion("Chrome", "Mozilla/5.0", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36", 28);
  </code></pre>
  
  <p>By the way, when I instantiate a Webclient object passing my <code>bv</code> object, my code doesn't work anymore. From what I've understood, HtmlUnit documentation says that I've got to check if the user agent specified in my BrowserVersion has proper features to run Javascript. </p>
  
  <blockquote>
    <p>However, note that the constants are not enough to fully customize the
    browser, you also need to look into the BrowserVersionFeatures and the
    classes inside "javascript" package.</p>
  </blockquote>
  
  <p>What does it mean? Why HtmlUnit doesn't work anymore? My goal is only to change the User Agent string. How can I achieve this? Note that I've tried Selenium too, without success.
  Thank you for your help.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Found this trick. If I instantiate the BrowserVersion as follow:</p>
  
  <pre><code>BrowserVersion bv = new BrowserVersion("Netscape", "blablabla", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36", 0);
  </code></pre>
  
  <p>it works, but I don't understand WHY. I have to set the first string as Netscape (tried Chrome and Mozilla but it doesn't work). The 2nd string is random, I can put anything if Netscape is set as first parameter. The third string is the well formatted User Agent and the 4th parameter is an integer indicating the version. Can you explain me why it works only if <code>Netscape</code> is passed as first parameter and random to others (except the 2nd)?</p>
  
  <p><strong>UPDATES:</strong></p>
  
  <p>Sometimes it doesn't work (as described above). For some User Agents strings, the page is not correctly loading. I cannot understand why the User Agent should modify the behaviour of HtmlUnit as I'm pretty sure that Javascript it's quite easy and should be run by all browser versions. So, my <strong>final question</strong> is: how can I change the user agent string in HtmlUnit without altering its behaviour when executing Javascript?</p>