I have a large number of tests to write that use WWW::Mechanize to test web page sequences. These sequences are exercized by filling in form variables and POSTing. The values of form variables, along with other variables determine what the next page in the sequence will be and can also cause data to be shown or suppressed on later pages.

In writing these tests, I've been migrating to a data-driven structure like the code below. This has factored out some of the repetitive code, but I think there is room for further improvement. My goal is to end up with test scripts that are largely self-documenting in terms of what actions are being performed and what results are being tested and that have little repeated code.

Does anyone see a different approach that would be a better fit? Does anyone have any useful references on data-driven programming in perl? I have turned up very little with google and super-search.

... my $mech = WWW::Mechanize->new(...); my $question = { 1 => 'value_12345', 2 => 'value_12346', 3 => 'value_12347', }; my $test_list = [ { name => 'First page of sequence', check_list => [ # Things to compare content to on this page [ qr/Some text/, "test name" ], ], action => sub { # Action to perform from this page $mech->click; }, }, { name => 'page seen after first click', check_list => [ [ qr/Dst1/, "destination1" ], ], action => sub { $mech->submit_form( fields => { $question->{1} => 3, # Q1, choice 3 (radio button) }); }, }, { name => 'page seen after second POST', check_list => [ [ qr/Dst2/, "destination2" ], [ qr/more text/, "more text expected on this page" ], ], action => sub { $mech->submit_form( fields => { $mech->back; $mech->back; }); }, }, ]; $mech->get(qq[some_uri]); my $verbose = 0; for my $test(@$test_list) { my $name = $test->{name}; my $check_list = $test->{check_list}; for my $check(@$check_list) { like($mech->content, $check->[0], "test $name - $check->[1]"); } diag $mech->current_form->dump if $verbose; if ($test->{action}) { $test->{action}->(); } sleep 1; # Optional }

In reply to Data-driven programming with WWW::Mechanize by mp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.