3 # $Id: maketest.pl,v 1.4 2004-09-06 08:30:44 rurban 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 [-b baseurl] <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 #use constant BASEURL => 'http://127.0.0.1/~swain/projects/phpwiki-1.3.x/';
24 my $baseurl = 'http://127.0.0.1/~swain/projects/phpwiki-1.3.x/';
28 # TODO: Get the BASEURL from config.ini
29 $baseurl = $opts{b} if $opts{b};
31 #print "passed in: ", join(" ", @ARGV), "\n";
32 my ($start_of_file, $end_of_file);
34 # read in the skeleton file from this script below the END tag
36 last if /PRINT_TEST_CODE/;
45 while ($inputfile = shift(@ARGV)) {
47 $inputfile =~ /(\w+)\.inputs$/;
48 my $classname = $1 || 'Test';
49 $start_of_file =~ s/__CLASSNAME__/$classname/g;
50 $end_of_file =~ s/__CLASSNAME__/$classname/g;
52 open OUTFILE, ">${classname}.java"
53 or die "Can't open '${classname}.java' for writing: $!\n";
56 # start each new file with a timestamp and user name
57 print OUTFILE "// This file was automatically generated on ",
58 scalar localtime(), " by $ENV{USER}\n";
61 print OUTFILE $start_of_file;
63 # read in the file in chunks with "go\n" as the record separator
66 open FILE, "<$inputfile"
69 unlink "${classname}.java";
70 die "Can't read config file '$inputfile': $!\n";
74 # set the response message/output... this is output after each "go" block
78 //System.out.println( "Here's the metadata for the response:" );
79 //System.out.println( "URL: " + response.getURL() );
80 //System.out.println( "Page title: " + response.getTitle() );
81 //System.out.println( "Response code: " + response.getResponseCode() );
82 //System.out.println( response );
83 //System.out.println( transaction_boundary );
88 # here is where we do the "parsing" of the .inputs file.
89 while ($block = <FILE>) {
91 next unless $block =~ /go$/;
93 if ($block =~ /type:\s*starting_page/) {
94 starting_page($block);
95 } elsif ($block =~ /type:\s*fill_and_submit_form/) {
96 fill_and_submit_form($block);
97 } elsif ($block =~ /type:\s*follow_link/) {
99 } elsif ($block =~ /type:\s*follow_image_link/) {
100 follow_image_link($block);
103 die "This block does not match any known action:\n$block\n";
108 print OUTFILE $end_of_file;
114 # End of main... subs are next, followed by the boilerplate code for the
115 # java files after the END thingie. What is that thingie called?
119 $block =~ m/start_url:\s*(http.*?)$/m;
120 my $start_url = $baseurl . $1;
121 my $assertions = &get_assertions($block);
123 print OUTFILE <<"EOLN"
125 System.out.println( "Name for this test: " + dealname );
127 // make a request object for the conversation object
129 myurl = "$start_url";
130 request = new GetMethodWebRequest( myurl );
131 response = conversation.getResponse( request );
132 } catch (Exception e) {
133 throw new Exception("Couldn't get a page from URL '$start_url'\\n" + e);
143 $block =~ m/follow_link:\s*(".*?")$/m;
145 my $assertions = &get_assertions($block);
147 print OUTFILE <<"EOLN";
149 // follow a plain link with text '$link_text'
150 linkname = $link_text;
151 link_to_follow = response.getLinkWith(linkname);
153 if (link_to_follow == null)
154 throw new Exception("The link '" + linkname + "' was not found.");
156 request = link_to_follow.getRequest();
157 System.out.println( request );
160 response = conversation.getResponse( request );
161 } catch (Exception r) {
162 throw new Exception(r + "\\nCouldn't follow the link!\\n" +
163 "Request was:\\n" + request + "\\n");
172 sub follow_image_link {
174 $block =~ m/follow_image_link:\s*(".*?")$/m;
176 my $assertions = &get_assertions($block);
178 print OUTFILE <<"EOLN";
180 // follow an image link with text '$link_name'
181 linkname = $link_name;
182 link_to_follow = response.getLinkWithImageText(linkname);
184 if (link_to_follow == null)
185 throw new Exception("The link '" + linkname + "' was not found.");
187 request = link_to_follow.getRequest();
188 System.out.println( request );
191 response = conversation.getResponse( request );
192 } catch (Exception r) {
193 throw new Exception(r + "\\nCouldn't follow the image link!\\n" +
194 "Request was:\\n" + request + "\\n");
203 sub fill_and_submit_form {
205 @lines = make_array_from_block($block);
207 my ($form_num, $submit_num, $form_name, $submit_name);
209 my $assertions = &get_assertions($block);
212 if ( /form_num:\s*(\d)/ ) {
214 } elsif ( /submitbutton_num:\s*(\d)/ ) {
216 } elsif ( /form_name:\s*(\w+)/ ) {
218 #print "form name: '$form_name'\n";
219 } elsif ( /submitbutton_name:\s*(\w+)/ ) {
221 #print "submit name: '$submit_name'\n";
222 } elsif ( /setparam:\s*(.+)$/ ) {
223 $requests .= " request.setParameter($1);\n";
226 unless ( (defined $form_num || defined $form_name)
227 && (defined $submit_num || defined $submit_name)
228 && defined $requests) {
232 form_num: '$form_num' (you need either form_num or form_name)
233 submit_num: '$submit_num' (you need either submit_num or submit_name)
234 form_name: '$form_name'
235 submit_name: '$submit_name'
236 requests: '$requests'
240 # provide a bit of minor error detection...
241 if ( defined $form_num && defined $form_name) {
244 You can't have both a form number and a form name defined
245 (got form_num '$form_num' and form_name '$form_name' for
250 if ( defined $submit_num && defined $submit_name) {
253 You can't have both a submit button number and a
254 submit button name defined
255 (got submit_num '$submit_num' and submit_name '$submit_name' for
260 if (defined $form_num) {
261 $form_code = <<"EOLN";
263 htmlforms = response.getForms();
265 if (htmlforms == null || htmlforms.length == 0)
266 throw new Exception("No HTML form found for:\\n" + response);
268 htmlform = htmlforms[$form_num];
270 } elsif (defined $form_name) {
271 $form_code = <<"EOLN";
273 htmlform = response.getFormWithName("$form_name");
274 if (htmlform == null)
275 throw new Exception("No HTML form named '$form_name' found for:\\n"
281 die "Didn't get a form_name or form_num for this block:\n$block\n";
285 if (defined $submit_num) {
286 $submit_button_code = <<"EOLN";
288 submitButtonArray = htmlform.getSubmitButtons();
290 if (submitButtonArray == null || submitButtonArray.length == 0)
291 throw new Exception("Didn't get a submit button array in "
292 + "response object:\\n");
295 request = htmlform.getRequest(submitButtonArray[$submit_num]);
297 } elsif (defined $submit_name) {
298 $submit_button_code = <<"EOLN";
300 submitbutton = htmlform.getSubmitButton("$submit_name");
301 if (submitbutton == null)
302 throw new Exception("Couldn't fine a submit button "
303 + "named '$submit_name'\\n"
305 request = htmlform.getRequest(submitbutton);
310 die "Didn't get a submit_num or submit_name for this block:\n$block\n";
313 print OUTFILE <<" EOLN";
315 // get and fill the HTML form
320 } catch (Exception n) {
321 throw new Exception( n
322 + "\\nCouldn't set a parameter in this request:\\n"
327 response = conversation.getResponse( request );
328 } catch (Exception r) {
329 throw new Exception(r + "\\nCouldn't submit the form!\\n" +
330 "Request was:\\n" + request + "\\n");
342 my $return_text = &assert_url($block);
343 $return_text .= &assert_title($block);
344 $return_text .= &assert_field($block);
345 $return_text .= &assert_text($block);
346 #print "Return text:\n$return_text\n";
353 return unless $block =~ /^assert_url:\s*(.*?)$/m;
360 if ( response.getURL().toString().indexOf( "$url" ) != -1)
361 System.out.println("\tURL match: matched '$url' OK");
363 throw new Exception ("URL match: Didn't match URL '$url' ERROR");
367 # assert that the title of the page is correct
371 return unless $block =~ /^assert_title:\s*(.*?)$/m;
377 // assert the page title
378 if ( response.getTitle().toString().indexOf( "$title" ) != -1)
379 System.out.println("\tTitle match: Matched '$title' OK");
381 throw new Exception("Title match: did not match title '$title' ERROR");
385 # assert that a form field matches a value
389 return unless $block =~ /assert_field/m;
391 my @lines = &make_array_from_block($block);
392 my $return_text = "";
394 foreach my $line (@lines) {
396 if ($line =~ /^assert_field:\s*(\d|"\w+")\s*("[^"]+")\s*("[^"]+")\s*$/) {
401 if ($form =~ /^\d+$/) {
402 $getform_string = "response.getForms()[$form].getParameterValue($field_name)";
404 $getform_string = "response.getFormWithName($form).getParameterValue($field_name)";
407 #print "$getform_string\n";
409 $return_text .= <<"EOLN";
411 // assertion: form '$form', field '$field_name' == '$field_val'
412 if ( ${field_val}.equals($getform_string) )
413 System.out.println("\tField match: '" + $field_name + "' held '" + $field_val + "' OK");
415 throw new Exception ("Field match: Field '" + $field_name + "' didn't match '" + $field_val + "' ERROR");
428 return unless $block =~ /assert_text/m;
430 my @lines = &make_array_from_block($block);
431 my $return_text = "";
433 foreach my $line (@lines) {
434 if ($line =~ /^assert_text:\s*(.*)$/) {
435 my $search_string = $1;
437 $return_text .= <<"EOLN";
439 // find the text string '$search_string' in the page source
440 if ( response.getText().indexOf( "$search_string" ) != -1)
441 System.out.println( "I found the text '$search_string' in the page OK" );
443 throw new Exception( "Couldn't find text: '$search_string'" );
453 sub make_array_from_block {
456 my @lines = split /\n/, $block;
457 foreach $line (@lines) {
458 next if $line =~ /^#/;
459 next unless $line =~ /\w/;
460 next if $line =~ /^go/;
461 push @return_array, $line;
463 return @return_array;
468 import com.meterware.httpunit.*;
470 import java.io.IOException;
471 import java.net.MalformedURLException;
475 import org.xml.sax.*;
477 public class __CLASSNAME__ {
479 public static void main( String[] params ) {
481 boolean success = true;
485 WebResponse response;
486 WebConversation conversation = new WebConversation();
489 SubmitButton[] submitButtonArray;
490 SubmitButton submitbutton;
491 WebLink link_to_follow;
494 String dealname = makeUniqueDealName("__CLASSNAME__ Test");
495 String transaction_boundary = "This is a transaction boundary.";
497 /* PRINT_TEST_CODE */
499 } catch (Exception e) {
500 System.err.println( "Exception: " + e );
503 if (success == true) {
504 System.out.println( "__CLASSNAME__ test successful." );
506 System.out.println( "__CLASSNAME__ test failed." );
513 public static String makeUniqueDealName(String dealname) {
514 Date today = new Date();
515 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
517 return dealname + " " + df.format(today);