3 # $Id: maketest.pl,v 1.3 2002-01-29 22:10:24 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
13 use constant BASEURL => 'http://127.0.0.1/~swain/projects/phpwiki-1.3.x/';
15 die <<"EOLN" unless $ARGV[0];
17 Usage: $0 <inputfile0> [ <inputfile1> <inputfile2> ... <inputfileN> ]
18 where 'inputfile' is the name of a configuration file that specifies
19 the form fields and values. The name of the file should be similar to
20 ClassName.inputs, and this script will produce a Java file called
24 #print "passed in: ", join(" ", @ARGV), "\n";
26 my ($start_of_file, $end_of_file);
28 # read in the skeleton file from this script below the END tag
30 last if /PRINT_TEST_CODE/;
39 while ($inputfile = shift(@ARGV)) {
41 $inputfile =~ /(\w+)\.inputs$/;
42 my $classname = $1 || 'Test';
43 $start_of_file =~ s/__CLASSNAME__/$classname/g;
44 $end_of_file =~ s/__CLASSNAME__/$classname/g;
46 open OUTFILE, ">${classname}.java"
47 or die "Can't open '${classname}.java' for writing: $!\n";
50 # start each new file with a timestamp and user name
51 print OUTFILE "// This file was automatically generated on ",
52 scalar localtime(), " by $ENV{USER}\n";
55 print OUTFILE $start_of_file;
57 # read in the file in chunks with "go\n" as the record separator
60 open FILE, "<$inputfile"
63 unlink "${classname}.java";
64 die "Can't read config file '$inputfile': $!\n";
68 # set the response message/output... this is output after each "go" block
72 //System.out.println( "Here's the metadata for the response:" );
73 //System.out.println( "URL: " + response.getURL() );
74 //System.out.println( "Page title: " + response.getTitle() );
75 //System.out.println( "Response code: " + response.getResponseCode() );
76 //System.out.println( response );
77 //System.out.println( transaction_boundary );
82 # here is where we do the "parsing" of the .inputs file.
83 while ($block = <FILE>) {
85 next unless $block =~ /go$/;
87 if ($block =~ /type:\s*starting_page/) {
88 starting_page($block);
89 } elsif ($block =~ /type:\s*fill_and_submit_form/) {
90 fill_and_submit_form($block);
91 } elsif ($block =~ /type:\s*follow_link/) {
93 } elsif ($block =~ /type:\s*follow_image_link/) {
94 follow_image_link($block);
97 die "This block does not match any known action:\n$block\n";
102 print OUTFILE $end_of_file;
108 # End of main... subs are next, followed by the boilerplate code for the
109 # java files after the END thingie. What is that thingie called?
113 $block =~ m/start_url:\s*(http.*?)$/m;
114 my $start_url = BASEURL . $1;
115 my $assertions = &get_assertions($block);
117 print OUTFILE <<"EOLN"
119 System.out.println( "Name for this test: " + dealname );
121 // make a request object for the conversation object
123 myurl = "$start_url";
124 request = new GetMethodWebRequest( myurl );
125 response = conversation.getResponse( request );
126 } catch (Exception e) {
127 throw new Exception("Couldn't get a page from URL '$start_url'\\n" + e);
137 $block =~ m/follow_link:\s*(".*?")$/m;
139 my $assertions = &get_assertions($block);
141 print OUTFILE <<"EOLN";
143 // follow a plain link with text '$link_text'
144 linkname = $link_text;
145 link_to_follow = response.getLinkWith(linkname);
147 if (link_to_follow == null)
148 throw new Exception("The link '" + linkname + "' was not found.");
150 request = link_to_follow.getRequest();
151 System.out.println( request );
154 response = conversation.getResponse( request );
155 } catch (Exception r) {
156 throw new Exception(r + "\\nCouldn't follow the link!\\n" +
157 "Request was:\\n" + request + "\\n");
166 sub follow_image_link {
168 $block =~ m/follow_image_link:\s*(".*?")$/m;
170 my $assertions = &get_assertions($block);
172 print OUTFILE <<"EOLN";
174 // follow an image link with text '$link_name'
175 linkname = $link_name;
176 link_to_follow = response.getLinkWithImageText(linkname);
178 if (link_to_follow == null)
179 throw new Exception("The link '" + linkname + "' was not found.");
181 request = link_to_follow.getRequest();
182 System.out.println( request );
185 response = conversation.getResponse( request );
186 } catch (Exception r) {
187 throw new Exception(r + "\\nCouldn't follow the image link!\\n" +
188 "Request was:\\n" + request + "\\n");
197 sub fill_and_submit_form {
199 @lines = make_array_from_block($block);
201 my ($form_num, $submit_num, $form_name, $submit_name);
203 my $assertions = &get_assertions($block);
206 if ( /form_num:\s*(\d)/ ) {
208 } elsif ( /submitbutton_num:\s*(\d)/ ) {
210 } elsif ( /form_name:\s*(\w+)/ ) {
212 #print "form name: '$form_name'\n";
213 } elsif ( /submitbutton_name:\s*(\w+)/ ) {
215 #print "submit name: '$submit_name'\n";
216 } elsif ( /setparam:\s*(.+)$/ ) {
217 $requests .= " request.setParameter($1);\n";
220 unless ( (defined $form_num || defined $form_name)
221 && (defined $submit_num || defined $submit_name)
222 && defined $requests) {
226 form_num: '$form_num' (you need either form_num or form_name)
227 submit_num: '$submit_num' (you need either submit_num or submit_name)
228 form_name: '$form_name'
229 submit_name: '$submit_name'
230 requests: '$requests'
234 # provide a bit of minor error detection...
235 if ( defined $form_num && defined $form_name) {
238 You can't have both a form number and a form name defined
239 (got form_num '$form_num' and form_name '$form_name' for
244 if ( defined $submit_num && defined $submit_name) {
247 You can't have both a submit button number and a
248 submit button name defined
249 (got submit_num '$submit_num' and submit_name '$submit_name' for
254 if (defined $form_num) {
255 $form_code = <<"EOLN";
257 htmlforms = response.getForms();
259 if (htmlforms == null || htmlforms.length == 0)
260 throw new Exception("No HTML form found for:\\n" + response);
262 htmlform = htmlforms[$form_num];
264 } elsif (defined $form_name) {
265 $form_code = <<"EOLN";
267 htmlform = response.getFormWithName("$form_name");
268 if (htmlform == null)
269 throw new Exception("No HTML form named '$form_name' found for:\\n"
275 die "Didn't get a form_name or form_num for this block:\n$block\n";
279 if (defined $submit_num) {
280 $submit_button_code = <<"EOLN";
282 submitButtonArray = htmlform.getSubmitButtons();
284 if (submitButtonArray == null || submitButtonArray.length == 0)
285 throw new Exception("Didn't get a submit button array in "
286 + "response object:\\n");
289 request = htmlform.getRequest(submitButtonArray[$submit_num]);
291 } elsif (defined $submit_name) {
292 $submit_button_code = <<"EOLN";
294 submitbutton = htmlform.getSubmitButton("$submit_name");
295 if (submitbutton == null)
296 throw new Exception("Couldn't fine a submit button "
297 + "named '$submit_name'\\n"
299 request = htmlform.getRequest(submitbutton);
304 die "Didn't get a submit_num or submit_name for this block:\n$block\n";
307 print OUTFILE <<" EOLN";
309 // get and fill the HTML form
314 } catch (Exception n) {
315 throw new Exception( n
316 + "\\nCouldn't set a parameter in this request:\\n"
321 response = conversation.getResponse( request );
322 } catch (Exception r) {
323 throw new Exception(r + "\\nCouldn't submit the form!\\n" +
324 "Request was:\\n" + request + "\\n");
336 my $return_text = &assert_url($block);
337 $return_text .= &assert_title($block);
338 $return_text .= &assert_field($block);
339 $return_text .= &assert_text($block);
340 #print "Return text:\n$return_text\n";
347 return unless $block =~ /^assert_url:\s*(.*?)$/m;
354 if ( response.getURL().toString().indexOf( "$url" ) != -1)
355 System.out.println("\tURL match: matched '$url' OK");
357 throw new Exception ("URL match: Didn't match URL '$url' ERROR");
361 # assert that the title of the page is correct
365 return unless $block =~ /^assert_title:\s*(.*?)$/m;
371 // assert the page title
372 if ( response.getTitle().toString().indexOf( "$title" ) != -1)
373 System.out.println("\tTitle match: Matched '$title' OK");
375 throw new Exception("Title match: did not match title '$title' ERROR");
379 # assert that a form field matches a value
383 return unless $block =~ /assert_field/m;
385 my @lines = &make_array_from_block($block);
386 my $return_text = "";
388 foreach my $line (@lines) {
390 if ($line =~ /^assert_field:\s*(\d|"\w+")\s*("[^"]+")\s*("[^"]+")\s*$/) {
395 if ($form =~ /^\d+$/) {
396 $getform_string = "response.getForms()[$form].getParameterValue($field_name)";
398 $getform_string = "response.getFormWithName($form).getParameterValue($field_name)";
401 #print "$getform_string\n";
403 $return_text .= <<"EOLN";
405 // assertion: form '$form', field '$field_name' == '$field_val'
406 if ( ${field_val}.equals($getform_string) )
407 System.out.println("\tField match: '" + $field_name + "' held '" + $field_val + "' OK");
409 throw new Exception ("Field match: Field '" + $field_name + "' didn't match '" + $field_val + "' ERROR");
422 return unless $block =~ /assert_text/m;
424 my @lines = &make_array_from_block($block);
425 my $return_text = "";
427 foreach my $line (@lines) {
428 if ($line =~ /^assert_text:\s*(.*)$/) {
429 my $search_string = $1;
431 $return_text .= <<"EOLN";
433 // find the text string '$search_string' in the page source
434 if ( response.getText().indexOf( "$search_string" ) != -1)
435 System.out.println( "I found the text '$search_string' in the page OK" );
437 throw new Exception( "Couldn't find text: '$search_string'" );
447 sub make_array_from_block {
450 my @lines = split /\n/, $block;
451 foreach $line (@lines) {
452 next if $line =~ /^#/;
453 next unless $line =~ /\w/;
454 next if $line =~ /^go/;
455 push @return_array, $line;
457 return @return_array;
462 import com.meterware.httpunit.*;
464 import java.io.IOException;
465 import java.net.MalformedURLException;
469 import org.xml.sax.*;
471 public class __CLASSNAME__ {
473 public static void main( String[] params ) {
475 boolean success = true;
479 WebResponse response;
480 WebConversation conversation = new WebConversation();
483 SubmitButton[] submitButtonArray;
484 SubmitButton submitbutton;
485 WebLink link_to_follow;
488 String dealname = makeUniqueDealName("__CLASSNAME__ Test");
489 String transaction_boundary = "This is a transaction boundary.";
491 /* PRINT_TEST_CODE */
493 } catch (Exception e) {
494 System.err.println( "Exception: " + e );
497 if (success == true) {
498 System.out.println( "__CLASSNAME__ test successful." );
500 System.out.println( "__CLASSNAME__ test failed." );
507 public static String makeUniqueDealName(String dealname) {
508 Date today = new Date();
509 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
511 return dealname + " " + df.format(today);