Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl: the Markov chain saw
 
PerlMonks  

Data-driven programming with WWW::Mechanize

by mp (Deacon)
on Sep 08, 2004 at 18:01 UTC ( #389430=perlquestion: print w/ replies, xml ) Need Help??
mp has asked for the wisdom of the Perl Monks concerning the following question:

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 }

Comment on Data-driven programming with WWW::Mechanize
Download Code
Re: Data-driven programming with WWW::Mechanize
by johnnywang (Priest) on Sep 08, 2004 at 18:47 UTC
Re: Data-driven programming with WWW::Mechanize
by Aristotle (Chancellor) on Sep 08, 2004 at 23:31 UTC

    Have you seen Test::WWW::Mechanize? You can do much better if you adopt its use with a less data driven approach.

    my $question = { 1 => 'value_12345', 2 => 'value_12346', 3 => 'value_12347', }; my @test = ( 'First page of sequence' => sub { my ( $mech, $name ) = @_; $mech->get( qq[some_uri] ); $mech->content_like( qr/Some text/, $name ); $mech->click; }, 'page seen after first click' => sub { my ( $mech, $name ) = @_; $mech->content_like( qr/Dst1/, "$name: destination1" ); $mech->submit_form( fields => { $question->{ 1 } => 3, # Q1, choice 3 (radio button) } ); }, 'page seen after second POST' => sub { my ( $mech, $name ) = @_; $mech->content_like( qr/Dst2/, "$name: destination2" ], $mech->content_like( qr/more text/, "$name: more text expected +" ], $mech->submit_form( fields => { # ... } ); $mech->back; $mech->back; }, ); my $verbose = 0; my $mech = Test::WWW::Mechanize->new( ... ); while( my ( $name, $test ) = splice @test, 0, 2 ) { $test->( $mech, $name ); diag $mech->current_form->dump if $verbose; sleep 1; }

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://389430]
Approved by ikegami
Front-paged by RMGir
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-04-18 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls