[Webtest] An idea about HTML templates and Testing
Fri, 20 Dec 2002 14:20:08 +0900
So anyway, I had this idea. It comes partly from looking at Canoo Webtest and HttpUnit, and I'd be very interested to know if I could implement all of it in Canoo WebTest because superficially it seems like I can't. I will explain:
Recently I am doing a lot of work with html templates (using the Velocity Templating language).
Anyway, the thing is that I have lots of lots of web management screens of one sort or another. Each screen is backed by a template, and some associated code. I have a ControllerServlet passing off requests to particular templates and processing the associated code.
I do some of this in Turbine (which handle a lot of things automatically), and some of it not. Either way the paradigm is the same.
Recently I have acquired two habits that I believe are helping me produce working, effective management screens.
1. I define all HTML form variables in the code associated with the template
2. I use HttpUnit to check that the forms work properly
These two things are related. The function of the first is so that when I am checking a parameter from a submitted form, I know that I am checking for precisely the parameter specified in the form, e.g.
<form name="$EDIT_FORM" action="$FORM_ACTION" method="POST">
<input type="hidden" name="$ACTION" value="$ACTION_TYPE">
**Template generation Code segment**
public class AddBookmarkCommand
public static final String ACTION = "action";
public static final String EDIT_FORM = "edit_form";
public static final String FORM_ACTION = "/neurogrid/servlet/forum";
public static final String ACTION_TYPE = "add_bookmark";
public void generateTemplate(VelocityContext p_context)
**Form handling code segment**
public class AddBookmarkCommand
String x_action = request.getParameter(ACTION);
// do some stuff
Anyway, I'm not sure how clear the above example is, and it probably helps if you know Velocity, but the point is that when I am checking for attribute/value pairs coming in from html form submissions I know the form has been supplied with precisely those values.
The HttpUnit code goes something like this:
WebResponse x_jdoc = o_wc.getCurrentPage();
WebForm x_form = x_jdoc.getFormWithName(AddBookmarkCommand.EDIT_FORM);
So perhaps you can see the relation. I am also able to access the static final HTML form attributes in the test code to ensure that there is no confusion. Everything that is going to interact with these attribute/value pairs is refering to precisely the same things.
So one thing I am interested in is getting some feedback on the above approach.
The other thing is that I am thinking about taking all this a step further. It would seem possible to write some code that would take an html template and automatically generate java code that would serve as the form handling code as well as the testing code. So you write your template, and then press a button, and hey presto you have a form handling class that will specify all the necessary final static variables, and testing code that will check if they are present when you view the template.
Canoo WebTest seems to do something like this. However I don't see anyway to link the attribute/value pairs throughout, in the way I describe above.
Alot of my work involves writing and testing web management screens and it would be great to be able to streamline the process.
However maybe I am overlooking some serious flaws in my appoach? Any