3 # $Id: maketest.pl,v 1.2 2001-09-21 01:04:54 wainstead Exp $
5 # read in a file, generate Java code to run a test.
6 # Steve Wainstead, March 2001.
8 # Naturally, this is not a recursive descent parser (sorry to disappoint you)
9 # but a fairly cut and dry script relying on "if" clauses to parse the input
10 # files. It was the shortest route to the answer for now, though certainly not
14 die <<"EOLN" unless $ARGV[0];
16 Usage: $0 <inputfile0> [ <inputfile1> <inputfile2> ... <inputfileN> ]
17 where 'inputfile' is the name of a configuration file that specifies
18 the form fields and values. The name of the file should be similar to
19 ClassName.inputs, and this script will produce a Java file called
23 #print "passed in: ", join(" ", @ARGV), "\n";
25 my ($start_of_file, $end_of_file);
27 # read in the skeleton file from this script below the END tag
29 last if /PRINT_TEST_CODE/;
38 while ($inputfile = shift(@ARGV)) {
40 $inputfile =~ /(\w+)\.inputs$/;
41 my $classname = $1 || 'Test';
42 $start_of_file =~ s/__CLASSNAME__/$classname/g;
43 $end_of_file =~ s/__CLASSNAME__/$classname/g;
45 open OUTFILE, ">${classname}.java"
46 or die "Can't open '${classname}.java' for writing: $!\n";
49 # start each new file with a timestamp and user name
50 print OUTFILE "// This file was automatically generated on ",
51 scalar localtime(), " by $ENV{USER}\n";
54 print OUTFILE $start_of_file;
56 # read in the file in chunks with "go\n" as the record separator
59 open FILE, "<$inputfile"
62 unlink "${classname}.java";
63 die "Can't read config file '$inputfile': $!\n";
67 # set the response message/output... this is output after each "go" block
71 //System.out.println( "Here's the metadata for the response:" );
72 //System.out.println( "URL: " + response.getURL() );
73 //System.out.println( "Page title: " + response.getTitle() );
74 //System.out.println( "Response code: " + response.getResponseCode() );
75 //System.out.println( response );
76 //System.out.println( transaction_boundary );
81 # here is where we do the "parsing" of the .inputs file.
82 while ($block = <FILE>) {
84 next unless $block =~ /go$/;
86 if ($block =~ /type:\s*starting_page/) {
87 starting_page($block);
88 } elsif ($block =~ /type:\s*fill_and_submit_form/) {
89 fill_and_submit_form($block);
90 } elsif ($block =~ /type:\s*follow_link/) {
92 } elsif ($block =~ /type:\s*follow_image_link/) {
93 follow_image_link($block);
96 die "This block does not match any known action:\n$block\n";
101 print OUTFILE $end_of_file;
107 # End of main... subs are next, followed by the boilerplate code for the
108 # java files after the END thingie. What is that thingie called?
112 $block =~ m/start_url:\s*(http.*?)$/m;
114 my $assertions = &get_assertions($block);
116 print OUTFILE <<"EOLN"
118 System.out.println( "Name for this test: " + dealname );
120 // make a request object for the conversation object
122 myurl = "$start_url";
123 request = new GetMethodWebRequest( myurl );
124 response = conversation.getResponse( request );
125 } catch (Exception e) {
126 throw new Exception("Couldn't get a page from URL '$start_url'\\n" + e);
136 $block =~ m/follow_link:\s*(".*?")$/m;
138 my $assertions = &get_assertions($block);
140 print OUTFILE <<"EOLN";
142 // follow a plain link with text '$link_text'
143 linkname = $link_text;
144 link_to_follow = response.getLinkWith(linkname);
146 if (link_to_follow == null)
147 throw new Exception("The link '" + linkname + "' was not found.");
149 request = link_to_follow.getRequest();
150 System.out.println( request );
153 response = conversation.getResponse( request );
154 } catch (Exception r) {
155 throw new Exception(r + "\\nCouldn't follow the link!\\n" +
156 "Request was:\\n" + request + "\\n");
165 sub follow_image_link {
167 $block =~ m/follow_image_link:\s*(".*?")$/m;
169 my $assertions = &get_assertions($block);
171 print OUTFILE <<"EOLN";
173 // follow an image link with text '$link_name'
174 linkname = $link_name;
175 link_to_follow = response.getLinkWithImageText(linkname);
177 if (link_to_follow == null)
178 throw new Exception("The link '" + linkname + "' was not found.");
180 request = link_to_follow.getRequest();
181 System.out.println( request );
184 response = conversation.getResponse( request );
185 } catch (Exception r) {
186 throw new Exception(r + "\\nCouldn't follow the image link!\\n" +
187 "Request was:\\n" + request + "\\n");
196 sub fill_and_submit_form {
198 @lines = make_array_from_block($block);
200 my ($form_num, $submit_num, $form_name, $submit_name);
202 my $assertions = &get_assertions($block);
205 if ( /form_num:\s*(\d)/ ) {
207 } elsif ( /submitbutton_num:\s*(\d)/ ) {
209 } elsif ( /form_name:\s*(\w+)/ ) {
211 #print "form name: '$form_name'\n";
212 } elsif ( /submitbutton_name:\s*(\w+)/ ) {
214 #print "submit name: '$submit_name'\n";
215 } elsif ( /setparam:\s*(.+)$/ ) {
216 $requests .= " request.setParameter($1);\n";
219 unless ( (defined $form_num || defined $form_name)
220 && (defined $submit_num || defined $submit_name)
221 && defined $requests) {
225 form_num: '$form_num' (you need either form_num or form_name)
226 submit_num: '$submit_num' (you need either submit_num or submit_name)
227 form_name: '$form_name'
228 submit_name: '$submit_name'
229 requests: '$requests'
233 # provide a bit of minor error detection...
234 if ( defined $form_num && defined $form_name) {
237 You can't have both a form number and a form name defined
238 (got form_num '$form_num' and form_name '$form_name' for
243 if ( defined $submit_num && defined $submit_name) {
246 You can't have both a submit button number and a
247 submit button name defined
248 (got submit_num '$submit_num' and submit_name '$submit_name' for
253 if (defined $form_num) {
254 $form_code = <<"EOLN";
256 htmlforms = response.getForms();
258 if (htmlforms == null || htmlforms.length == 0)
259 throw new Exception("No HTML form found for:\\n" + response);
261 htmlform = htmlforms[$form_num];
263 } elsif (defined $form_name) {
264 $form_code = <<"EOLN";
266 htmlform = response.getFormWithName("$form_name");
267 if (htmlform == null)
268 throw new Exception("No HTML form named '$form_name' found for:\\n"
274 die "Didn't get a form_name or form_num for this block:\n$block\n";
278 if (defined $submit_num) {
279 $submit_button_code = <<"EOLN";
281 submitButtonArray = htmlform.getSubmitButtons();
283 if (submitButtonArray == null || submitButtonArray.length == 0)
284 throw new Exception("Didn't get a submit button array in "
285 + "response object:\\n");
288 request = htmlform.getRequest(submitButtonArray[$submit_num]);
290 } elsif (defined $submit_name) {
291 $submit_button_code = <<"EOLN";
293 submitbutton = htmlform.getSubmitButton("$submit_name");
294 if (submitbutton == null)
295 throw new Exception("Couldn't fine a submit button "
296 + "named '$submit_name'\\n"
298 request = htmlform.getRequest(submitbutton);
303 die "Didn't get a submit_num or submit_name for this block:\n$block\n";
306 print OUTFILE <<" EOLN";
308 // get and fill the HTML form
313 } catch (Exception n) {
314 throw new Exception( n
315 + "\\nCouldn't set a parameter in this request:\\n"
320 response = conversation.getResponse( request );
321 } catch (Exception r) {
322 throw new Exception(r + "\\nCouldn't submit the form!\\n" +
323 "Request was:\\n" + request + "\\n");
335 my $return_text = &assert_url($block);
336 $return_text .= &assert_title($block);
337 $return_text .= &assert_field($block);
338 $return_text .= &assert_text($block);
339 #print "Return text:\n$return_text\n";
346 return unless $block =~ /^assert_url:\s*(.*?)$/m;
353 if ( response.getURL().toString().indexOf( "$url" ) != -1)
354 System.out.println("\tURL match: matched '$url' OK");
356 throw new Exception ("URL match: Didn't match URL '$url' ERROR");
360 # assert that the title of the page is correct
364 return unless $block =~ /^assert_title:\s*(.*?)$/m;
370 // assert the page title
371 if ( response.getTitle().toString().indexOf( "$title" ) != -1)
372 System.out.println("\tTitle match: Matched '$title' OK");
374 throw new Exception("Title match: did not match title '$title' ERROR");
378 # assert that a form field matches a value
382 return unless $block =~ /assert_field/m;
384 my @lines = &make_array_from_block($block);
385 my $return_text = "";
387 foreach my $line (@lines) {
389 if ($line =~ /^assert_field:\s*(\d|"\w+")\s*("[^"]+")\s*("[^"]+")\s*$/) {
394 if ($form =~ /^\d+$/) {
395 $getform_string = "response.getForms()[$form].getParameterValue($field_name)";
397 $getform_string = "response.getFormWithName($form).getParameterValue($field_name)";
400 #print "$getform_string\n";
402 $return_text .= <<"EOLN";
404 // assertion: form '$form', field '$field_name' == '$field_val'
405 if ( ${field_val}.equals($getform_string) )
406 System.out.println("\tField match: '" + $field_name + "' held '" + $field_val + "' OK");
408 throw new Exception ("Field match: Field '" + $field_name + "' didn't match '" + $field_val + "' ERROR");
421 return unless $block =~ /assert_text/m;
423 my @lines = &make_array_from_block($block);
424 my $return_text = "";
426 foreach my $line (@lines) {
427 if ($line =~ /^assert_text:\s*(.*)$/) {
428 my $search_string = $1;
430 $return_text .= <<"EOLN";
432 // find the text string '$search_string' in the page source
433 if ( response.getText().indexOf( "$search_string" ) != -1)
434 System.out.println( "I found the text '$search_string' in the page OK" );
436 throw new Exception( "Couldn't find text: '$search_string'" );
446 sub make_array_from_block {
449 my @lines = split /\n/, $block;
450 foreach $line (@lines) {
451 next if $line =~ /^#/;
452 next unless $line =~ /\w/;
453 next if $line =~ /^go/;
454 push @return_array, $line;
456 return @return_array;
461 import com.meterware.httpunit.*;
463 import java.io.IOException;
464 import java.net.MalformedURLException;
468 import org.xml.sax.*;
470 public class __CLASSNAME__ {
472 public static void main( String[] params ) {
474 boolean success = true;
478 WebResponse response;
479 WebConversation conversation = new WebConversation();
482 SubmitButton[] submitButtonArray;
483 SubmitButton submitbutton;
484 WebLink link_to_follow;
487 String dealname = makeUniqueDealName("__CLASSNAME__ Test");
488 String transaction_boundary = "This is a transaction boundary.";
490 /* PRINT_TEST_CODE */
492 } catch (Exception e) {
493 System.err.println( "Exception: " + e );
496 if (success == true) {
497 System.out.println( "__CLASSNAME__ test successful." );
499 System.out.println( "__CLASSNAME__ test failed." );
506 public static String makeUniqueDealName(String dealname) {
507 Date today = new Date();
508 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
510 return dealname + " " + df.format(today);