HTMLUnit : super slow execution?

Kanishka Dilshan picture Kanishka Dilshan · May 4, 2012 · Viewed 10.8k times · Source

I have been using HTMLUnit . It suits my requirements well. But it seems to be extremely slow. for example : I have automated the following scenario using HTMLUnit

Goto Google page
Enter some text
Click on the search button
Get the title of the results page
Click on the first result.

Code :

long t1=System.currentTimeMillis();
Logger logger=Logger.getLogger("");
logger.setLevel(Level.OFF);
WebClient webClient=createWebClient();
WebRequest webReq=new WebRequest(new URL("http://google.lk"));

HtmlPage googleMainPage=webClient.getPage(webReq);
HtmlTextInput searchTextField=(HtmlTextInput) googleMainPage.getByXPath("//input[@name='q']").get(0);
HtmlButton searchButton=(HtmlButton) googleMainPage.getByXPath("//button[@name='btnK']").get(0);

searchTextField.type("Sri Lanka");
System.out.println("Text typed!");
HtmlPage googleResultsPage= searchButton.click();
System.out.println("Search button clicked!");

System.out.println("Title : " + googleResultsPage.getTitleText());

HtmlAnchor firstResultLink=(HtmlAnchor) googleResultsPage.getByXPath("//a[@class='l']").get(0);
HtmlPage firstResultPage=firstResultLink.click();
System.out.println("First result clicked!");

System.out.println("Title : " + firstResultPage.getTitleText());
//System.out.println(firstResultPage.asText());
long t2=System.currentTimeMillis();
long diff=t2-t1;
System.out.println("Time elapsed : "  + milliSecondsToHrsMinutesAndSeconds(diff));

webClient.closeAllWindows();

It works 100% well. But it takes 3 minutes,41 seconds

I guess the reason for the slow execution is validating each and every element on the page.

My question is how to reduce the execution time of HTMLUnit ? is there any way to disable validations on webpages.

Thanks in advance!

Answer

fstang picture fstang · Nov 8, 2013

For the current htmlUnit 2.13, setting options is slightly different from what maxmax has provided:

final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false);//if you don't need css
webClient.getOptions().setJavaScriptEnabled(false);//if you don't need js
HtmlPage page = webClient.getPage("http://XXX.xxx.xx");
...

In my own test, this is 8 times faster than the default options.(Note that this could be webpage-dependent)