Activate your free membership today | Log-in

Thursday, March 22nd, 2007

Automatic testing of Ajax from Java

Category: Java, Testing

<p>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.

JAVA:
  1.  
  2. AjaxListener listener = new AjaxListener() {
  3. public void endAjax(Map eventMap) {
  4.           bitSet.flip(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal());
  5.           if (null != eventMap) {
  6.               bitSet.flip(TestFeature.HAS_MAP.ordinal());
  7.           }
  8.           // Make some assertions about the response text
  9.           String responseText = (String) eventMap.get("responseText");
  10.           if (null != responseText) {
  11.               if (-1 != responseText.indexOf("<partial -response>") &&
  12.                   -1 != responseText.indexOf("</partial>")) {
  13.                 bitSet.flip(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal());
  14.               }
  15.           }
  16.           Document responseXML = (Document)
  17.               eventMap.get("responseXML");
  18.           Element rootElement = null, element = null;
  19.           Node node = null;
  20.           String tagName = null;
  21.           try {
  22.               rootElement = responseXML.getDocumentElement();
  23.               tagName = rootElement.getTagName();
  24.               if (tagName.equals("partial-response")) {
  25.                   element = (Element) rootElement.getFirstChild();
  26.                   tagName = element.getTagName();
  27. ...
  28.  

Check out the two part screencast of this in action:

Part One, Part Two.

Related Content:

  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Microsoft passes OpenAjax tests
    Microsoft announced it passed the OpenAjax InteropFest 1.0 tests and is interoperable with other Ajax components in the OpenAjax ecosystem through the...
  • Ajax tools and products
    New tools for Ajax and RIA developers appear regularly. Here is a sampling of some, including a review of free Ajax testing tools and a detailed look...
  • Ajax Learning Guide
    Chances are, you've been doing JavaScript and XML developer work in Lotus Domino for quite some time. This old/new approach is causing quite a stir in...

Posted by Dion Almaer at 2:39 pm
5 Comments

+++--
3.8 rating from 22 votes

5 Comments »

Comments feed TrackBack URI

Would this be similiar to HTMLUnit? Would there be advantages to this over HTMLUnit?

Comment by Kris Zyp — March 23, 2007

It fills this requirement which htmlunit currently does not:

Allows writing tests that make assertions about the response to Ajax requests.

Comment by Ed Burns — March 23, 2007

Ed,
How does this relate to JRex?

Comment by Patrick Lightbody — March 23, 2007

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?

Comment by toy — October 12, 2007

How is this different from other testing scripts?

Comment by portraits — February 8, 2008

Leave a comment

You must be logged in to post a comment.