2 # read in a file, generate Java code to run a test.
3 # Steve Wainstead, March 2001.
5 # Naturally, this is not a recursive descent parser (sorry to disappoint you)
6 # but a fairly cut and dry script relying on "if" clauses to parse the input
7 # files. It was the shortest route to the answer for now, though certainly not
11 die <<"EOLN" unless $ARGV[0];
13 Usage: $0 [-b baseurl] <inputfile0> [ <inputfile1> <inputfile2> ... <inputfileN> ]
14 where 'inputfile' is the name of a configuration file that specifies
15 the form fields and values. The name of the file should be similar to
16 ClassName.inputs, and this script will produce a Java file called
20 #use constant BASEURL => 'http://127.0.0.1/~swain/projects/phpwiki-1.3.x/';
21 my $baseurl = 'http://127.0.0.1/~swain/projects/phpwiki-1.3.x/';
25 # TODO: Get the BASEURL from config.ini
26 $baseurl = $opts{b} if $opts{b};
28 #print "passed in: ", join(" ", @ARGV), "\n";
29 my ($start_of_file, $end_of_file);
31 # read in the skeleton file from this script below the END tag
33 last if /PRINT_TEST_CODE/;
42 while ($inputfile = shift(@ARGV)) {
44 $inputfile =~ /(\w+)\.inputs$/;
45 my $classname = $1 || 'Test';
46 $start_of_file =~ s/__CLASSNAME__/$classname/g;
47 $end_of_file =~ s/__CLASSNAME__/$classname/g;
49 open OUTFILE, ">${classname}.java"
50 or die "Can't open '${classname}.java' for writing: $!\n";
53 # start each new file with a timestamp and user name
54 print OUTFILE "// This file was automatically generated on ",
55 scalar localtime(), " by $ENV{USER}\n";
58 print OUTFILE $start_of_file;
60 # read in the file in chunks with "go\n" as the record separator
63 open FILE, "<$inputfile"
66 unlink "${classname}.java";
67 die "Can't read config file '$inputfile': $!\n";
71 # set the response message/output... this is output after each "go" block
75 //System.out.println( "Here's the metadata for the response:" );
76 //System.out.println( "URL: " + response.getURL() );
77 //System.out.println( "Page title: " + response.getTitle() );
78 //System.out.println( "Response code: " + response.getResponseCode() );
79 //System.out.println( response );
80 //System.out.println( transaction_boundary );
85 # here is where we do the "parsing" of the .inputs file.
86 while ($block = <FILE>) {
88 next unless $block =~ /go$/;
90 if ($block =~ /type:\s*starting_page/) {
91 starting_page($block);
92 } elsif ($block =~ /type:\s*fill_and_submit_form/) {
93 fill_and_submit_form($block);
94 } elsif ($block =~ /type:\s*follow_link/) {
96 } elsif ($block =~ /type:\s*follow_image_link/) {
97 follow_image_link($block);
100 die "This block does not match any known action:\n$block\n";
105 print OUTFILE $end_of_file;
111 # End of main... subs are next, followed by the boilerplate code for the
112 # java files after the END thingie. What is that thingie called?
116 $block =~ m/start_url:\s*(http.*?)$/m;
117 my $start_url = $baseurl . $1;
118 my $assertions = &get_assertions($block);
120 print OUTFILE <<"EOLN"
122 System.out.println( "Name for this test: " + dealname );
124 // make a request object for the conversation object
126 myurl = "$start_url";
127 request = new GetMethodWebRequest( myurl );
128 response = conversation.getResponse( request );
129 } catch (Exception e) {
130 throw new Exception("Couldn't get a page from URL '$start_url'\\n" + e);
140 $block =~ m/follow_link:\s*(".*?")$/m;
142 my $assertions = &get_assertions($block);
144 print OUTFILE <<"EOLN";
146 // follow a plain link with text '$link_text'
147 linkname = $link_text;
148 link_to_follow = response.getLinkWith(linkname);
150 if (link_to_follow == null)
151 throw new Exception("The link '" + linkname + "' was not found.");
153 request = link_to_follow.getRequest();
154 System.out.println( request );
157 response = conversation.getResponse( request );
158 } catch (Exception r) {
159 throw new Exception(r + "\\nCouldn't follow the link!\\n" +
160 "Request was:\\n" + request + "\\n");
169 sub follow_image_link {
171 $block =~ m/follow_image_link:\s*(".*?")$/m;
173 my $assertions = &get_assertions($block);
175 print OUTFILE <<"EOLN";
177 // follow an image link with text '$link_name'
178 linkname = $link_name;
179 link_to_follow = response.getLinkWithImageText(linkname);
181 if (link_to_follow == null)
182 throw new Exception("The link '" + linkname + "' was not found.");
184 request = link_to_follow.getRequest();
185 System.out.println( request );
188 response = conversation.getResponse( request );
189 } catch (Exception r) {
190 throw new Exception(r + "\\nCouldn't follow the image link!\\n" +
191 "Request was:\\n" + request + "\\n");
200 sub fill_and_submit_form {
202 @lines = make_array_from_block($block);
204 my ($form_num, $submit_num, $form_name, $submit_name);
206 my $assertions = &get_assertions($block);
209 if ( /form_num:\s*(\d)/ ) {
211 } elsif ( /submitbutton_num:\s*(\d)/ ) {
213 } elsif ( /form_name:\s*(\w+)/ ) {
215 #print "form name: '$form_name'\n";
216 } elsif ( /submitbutton_name:\s*(\w+)/ ) {
218 #print "submit name: '$submit_name'\n";
219 } elsif ( /setparam:\s*(.+)$/ ) {
220 $requests .= " request.setParameter($1);\n";
223 unless ( (defined $form_num || defined $form_name)
224 && (defined $submit_num || defined $submit_name)
225 && defined $requests) {
229 form_num: '$form_num' (you need either form_num or form_name)
230 submit_num: '$submit_num' (you need either submit_num or submit_name)
231 form_name: '$form_name'
232 submit_name: '$submit_name'
233 requests: '$requests'
237 # provide a bit of minor error detection...
238 if ( defined $form_num && defined $form_name) {
241 You can't have both a form number and a form name defined
242 (got form_num '$form_num' and form_name '$form_name' for
247 if ( defined $submit_num && defined $submit_name) {
250 You can't have both a submit button number and a
251 submit button name defined
252 (got submit_num '$submit_num' and submit_name '$submit_name' for
257 if (defined $form_num) {
258 $form_code = <<"EOLN";
260 htmlforms = response.getForms();
262 if (htmlforms == null || htmlforms.length == 0)
263 throw new Exception("No HTML form found for:\\n" + response);
265 htmlform = htmlforms[$form_num];
267 } elsif (defined $form_name) {
268 $form_code = <<"EOLN";
270 htmlform = response.getFormWithName("$form_name");
271 if (htmlform == null)
272 throw new Exception("No HTML form named '$form_name' found for:\\n"
278 die "Didn't get a form_name or form_num for this block:\n$block\n";
282 if (defined $submit_num) {
283 $submit_button_code = <<"EOLN";
285 submitButtonArray = htmlform.getSubmitButtons();
287 if (submitButtonArray == null || submitButtonArray.length == 0)
288 throw new Exception("Didn't get a submit button array in "
289 + "response object:\\n");
292 request = htmlform.getRequest(submitButtonArray[$submit_num]);
294 } elsif (defined $submit_name) {
295 $submit_button_code = <<"EOLN";
297 submitbutton = htmlform.getSubmitButton("$submit_name");
298 if (submitbutton == null)
299 throw new Exception("Couldn't fine a submit button "
300 + "named '$submit_name'\\n"
302 request = htmlform.getRequest(submitbutton);
307 die "Didn't get a submit_num or submit_name for this block:\n$block\n";
310 print OUTFILE <<" EOLN";
312 // get and fill the HTML form
317 } catch (Exception n) {
318 throw new Exception( n
319 + "\\nCouldn't set a parameter in this request:\\n"
324 response = conversation.getResponse( request );
325 } catch (Exception r) {
326 throw new Exception(r + "\\nCouldn't submit the form!\\n" +
327 "Request was:\\n" + request + "\\n");
339 my $return_text = &assert_url($block);
340 $return_text .= &assert_title($block);
341 $return_text .= &assert_field($block);
342 $return_text .= &assert_text($block);
343 #print "Return text:\n$return_text\n";
350 return unless $block =~ /^assert_url:\s*(.*?)$/m;
357 if ( response.getURL().toString().indexOf( "$url" ) != -1)
358 System.out.println("\tURL match: matched '$url' OK");
360 throw new Exception ("URL match: Didn't match URL '$url' ERROR");
364 # assert that the title of the page is correct
368 return unless $block =~ /^assert_title:\s*(.*?)$/m;
374 // assert the page title
375 if ( response.getTitle().toString().indexOf( "$title" ) != -1)
376 System.out.println("\tTitle match: Matched '$title' OK");
378 throw new Exception("Title match: did not match title '$title' ERROR");
382 # assert that a form field matches a value
386 return unless $block =~ /assert_field/m;
388 my @lines = &make_array_from_block($block);
389 my $return_text = "";
391 foreach my $line (@lines) {
393 if ($line =~ /^assert_field:\s*(\d|"\w+")\s*("[^"]+")\s*("[^"]+")\s*$/) {
398 if ($form =~ /^\d+$/) {
399 $getform_string = "response.getForms()[$form].getParameterValue($field_name)";
401 $getform_string = "response.getFormWithName($form).getParameterValue($field_name)";
404 #print "$getform_string\n";
406 $return_text .= <<"EOLN";
408 // assertion: form '$form', field '$field_name' == '$field_val'
409 if ( ${field_val}.equals($getform_string) )
410 System.out.println("\tField match: '" + $field_name + "' held '" + $field_val + "' OK");
412 throw new Exception ("Field match: Field '" + $field_name + "' didn't match '" + $field_val + "' ERROR");
425 return unless $block =~ /assert_text/m;
427 my @lines = &make_array_from_block($block);
428 my $return_text = "";
430 foreach my $line (@lines) {
431 if ($line =~ /^assert_text:\s*(.*)$/) {
432 my $search_string = $1;
434 $return_text .= <<"EOLN";
436 // find the text string '$search_string' in the page source
437 if ( response.getText().indexOf( "$search_string" ) != -1)
438 System.out.println( "I found the text '$search_string' in the page OK" );
440 throw new Exception( "Couldn't find text: '$search_string'" );
450 sub make_array_from_block {
453 my @lines = split /\n/, $block;
454 foreach $line (@lines) {
455 next if $line =~ /^#/;
456 next unless $line =~ /\w/;
457 next if $line =~ /^go/;
458 push @return_array, $line;
460 return @return_array;
465 import com.meterware.httpunit.*;
467 import java.io.IOException;
468 import java.net.MalformedURLException;
472 import org.xml.sax.*;
474 public class __CLASSNAME__ {
476 public static void main( String[] params ) {
478 boolean success = true;
482 WebResponse response;
483 WebConversation conversation = new WebConversation();
486 SubmitButton[] submitButtonArray;
487 SubmitButton submitbutton;
488 WebLink link_to_follow;
491 String dealname = makeUniqueDealName("__CLASSNAME__ Test");
492 String transaction_boundary = "This is a transaction boundary.";
494 /* PRINT_TEST_CODE */
496 } catch (Exception e) {
497 System.err.println( "Exception: " + e );
500 if (success == true) {
501 System.out.println( "__CLASSNAME__ test successful." );
503 System.out.println( "__CLASSNAME__ test failed." );
510 public static String makeUniqueDealName(String dealname) {
511 Date today = new Date();
512 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
514 return dealname + " " + df.format(today);