drag & drop now works for HtmlUnit

I wanted since a long time to be able to test drag & drop features with HtmlUnit like those of demo shop.

This was a long work to implement all missing js functionalities. Particularly’ drag & drop requires that information on elements’ position in the page are available but HtmlUnit doesn’t compute any rendering information (what makes it very fast). The solution was to compute faked positioning information: the position isn’t important by itself, it just needs to be coherent with the information of the mouse event.

The result is that it is now possible to test drag & drop on the demo shop:

public void testDragAndDrop() throws Exception
final String url = "";
final WebClient webClient = new WebClient();
webClient.setAjaxController(new NicelyResynchronizingAjaxController());

final HtmlPage page = (HtmlPage) webClient.getPage(url);

final HtmlElement mug = page.getHtmlElementById("product_1");
final HtmlElement cart = page.getHtmlElementById("cart");
assertEquals("check that the basket is empty", 0,


assertEquals("check that the basket now contains 1 element", 1,

Quite simple, isn’t it?

Update 31.03.08:

I’ve changed the XPath expressions from //img to .//img for compatibility with upcoming HtmlUnit 2.0.


1 Comment

  1. October 4, 2007 at 4:48 pm

    […] libraries might or might not work when tested via HtmlUnit. As of the current version, Prototype,, DWR and jQuery are known to be supported fairly well, Dojo is a bit of an unknown, YUI is known to […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: