Thursday, March 22nd, 2007
Automatic testing of Ajax from Java
Ed Burns (of Sun and the JSF expert group) has created an automated testing framework for Ajax in Java called MCP.
This framework stems from an old, old Mozilla project called the Mozilla Web Client started in 1999 as a part of the Sun/AOL/Netscape alliance. The ambitious misson statement of the project is:
The webclient project aims to provide the premier browser-neutral Java API that enables generic web browsing capability. This capability includes, but is not limited to: web content rendering, navigation, a history mechanism, and progress notification. The actual capabilities implemented depend on the underlying browser implementation.
You can tie unit tests into browser ajax events by using an AjaxListener and getting access to items such as the responseText/responseXML/HTTP headers, and more.
-
-
AjaxListener listener = new AjaxListener() {
-
bitSet.flip(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal());
-
if (null != eventMap) {
-
bitSet.flip(TestFeature.HAS_MAP.ordinal());
-
}
-
// Make some assertions about the response text
-
if (null != responseText) {
-
if (-1 != responseText.indexOf("<partial -response>") &&
-
-1 != responseText.indexOf("</partial>")) {
-
bitSet.flip(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal());
-
}
-
}
-
eventMap.get("responseXML");
-
Node node = null;
-
String tagName = null;
-
try {
-
rootElement = responseXML.getDocumentElement();
-
tagName = rootElement.getTagName();
-
if (tagName.equals("partial-response")) {
-
tagName = element.getTagName();
-
...
-
Check out the two part screencast of this in action:












Would this be similiar to HTMLUnit? Would there be advantages to this over HTMLUnit?
It fills this requirement which htmlunit currently does not:
Allows writing tests that make assertions about the response to Ajax requests.
Ed,
How does this relate to JRex?
Hey, I remember reading about Mozilla Web Client years ago. As I recall, everyone was pretty excited about it, and then it more or less just went away. That seems very odd for Mozilla – usually they stick with something until it’s finished or they at least make it known they aren’t going to continue the project. Maybe I was just looking in the wrong place. Anyway, thank you for the information on this Ajax testing application. It sounds like it’s going to be very, very useful for anyone who uses Ajax. The code looks very interesting – thanks for listing it for us. Does this Ajax Java test have any disadvantages when compared to HTMLUnit, or does it do everything HTMLUnit does plus allow for making assertions about Ajax requests?
How is this different from other testing scripts?