What about JWebUnit support of JavaScript?

JWebUnit uses a testing engine to manipulate DOM and JavaScript. That's why JavaScript support depend on the testing engine. In the past, JWebUnit was based on HttpUnit. JavaScript support of HttpUnit is very poor.

Now, JWebUnit main testing engine is HtmlUnit. HtmlUnit understanding of JavaScript is quite good.


What can I do when JavaScript errors are thrown with JWebUnit (and HtmlUnit testing engine), but not in my browser?

First, check if your JavaScript is not browser specific. When possible, try to write a minimal test case that reproduce your error, and submit it to the HtmlUnit team to help them to improve HtmlUnit.

Finally, you can temporary disable JavaScript in your test:

public void testFoo() {
  ...//Problematic code
Have a look at


How can I parse JSON output using JWebUnit or HtmlUnit?

HtmlUnit, one of the underlying engines of JWebUnit, does not support direct execution of Javascript functions. You need to check beforehand that the Content-Type of the response is application/json, and then use a different library to parse it.

For example, Google Gson can be used to parse JSON, as follows:

WebResponse response = ((HtmlUnitTestingEngineImpl) JWebUnit.getTestingEngine()).getWebResponse();
// check content type
assertTrue("Response type should be application/json, was: " + response.getContentType(), "application/json".equals(response.getContentType()));

// parse JSON
Gson gson = new Gson();
String json = response.getContentAsString();
JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); // or JsonArray, depending on the JSON content

assertTrue("Object should have success field", obj.has("success"));
String success = gson.fromJson(obj.get("success"), String.class);
String username = gson.fromJson(obj.get("username"), String.class);
String type = gson.fromJson(obj.get("type"), String.class);
// etc



Can I use JWebUnit coupled with JUnitPerf to do load testing?

Yes, but you have to take care when writing your JUnitPerf test. You need to use a TestFactory:

int users = 10;
Test factory = new TestFactory(MyJWebUnitTest.class);
Test loadTest = new LoadTest(factory, users);


HtmlUnit testing engine

How can I change HtmlUnit refresh handler (default is ImmediateRefreshHandler)?

Warning: this is not something easily portable to other testing engine.

if (getTestingEngine() instanceof HtmlUnitTestingEngineImpl) {
	HtmlUnitTestingEngineImpl engine = (HtmlUnitTestingEngineImpl) getTestingEngine();			
	engine.setRefreshHandler(new ThreadedRefreshHandler());