Record and Play Back Browsing Sessions

     

Creating lengthy programs to test web applications might seem a bit tedious . The mech-dump utility that comes with WWW::Mechanize prints the names and elements of every form and provides some relief when searching for form and form element names . However, using that data in your tests means that you'll have to cut and paste multiple small blocks of code. Yuck.


Note: The mech-dump utility that comes with WWW:: Mechanize prints out everything that a WWW:: Mechanize object knows about a web page .

Relieve some of the hassle by using HTTP::Recorder to set up an HTTP proxy to record the pages you visit and the forms you fill out. As you browse, HTTP::Recorder saves each action as WWW::Mechanize code.

How do I do that?

Save the following listing as recorder.pl :


Note: At the time of this writing, HTTP::Recorder is incomplete, though it's still worth using as a base from which you can develop test files for web interaction .
 #!perl          use strict;     use warnings;          use HTTP::Recorder;     use HTTP::Proxy;          my $agent = HTTP::Recorder->new( file => "mech2.t", showwindow => 1 );          my $proxy = HTTP::Proxy->new(         port  => 4567,         agent => $agent,     );          $proxy->start(  ); 

Next, configure your browser's proxy settings to connect to your own machine as a proxy on port 4567, as Figure 7-1 shows. Don't forget to restore the original settings after you finish this lab!

Figure 7-1. Proxy connection settings in Mozilla Firefox

Now run recorder.pl . You won't see any output from the program while it's running, so don't hold your breath .

 $  perl recorder.pl  

Go to http://search.cpan.org/ in your browser. If everything went as planned, you'll see a pop-up window appear with Perl code!


Note: Using Mozilla Firefox or some other pop-up - blocking tool? Allow pop-ups while you're doing this lab to see HTTP::Recorder's window .

Search the CPAN for "gerbil counting" and click the Submit button, and then click on the Home link at the top. Search for something else and click Next once a page of results appears. As you're doing this, the pop-up window will refresh with every request to show updated Perl code. Figure 7-2 shows an example.

Figure 7-2. Pop-up window produced by HTTP::Recorder

What just happened ?

Running recorder.pl starts an HTTP proxy daemon that your browser uses to make requests . The proxy uses an HTTP::Recorder agent, which attempts to keep track of submitted forms and log the requests in the form of Perl code. It saves a logfile as mech2.t , which you specifed when creating the HTTP::Recorder object. Additionally, because showwindow is true, the proxy modifies the content of the requested page to display a pop-up window with the current contents of mech2.t .

The Perl code saved to mech2.t is actually a series of statements involving a hypothetical WWW::Mechanize object. You can add the object yourself:

  #!perl          use WWW::Mechanize;          my $agent = WWW::Mechanize->new( autocheck => 1 );  $agent->get("http://search.cpan.org/");     $agent->field("query", "gerbil counting");     $agent->submit_form(form_name => "f");          $agent->follow_link(text => "Home", n => "1");     $agent->field("query", "test");     $agent->submit_form(form_name => "f");          $agent->follow_link(text => "Next >>", n => "1"); 

In its current state, this program isn't very useful. If the CPAN Search Site ceases to function and you run this program, WWW::Mechanize won't be able to fill out the forms and will die. A better idea is to convert it to a test file, which is why you named the file with a .t suffix. Modify mech2.t to use Test::WWW::Mechanize (from the "Testing Your Frontend" lab, earlier in this chapter):

  #!perl  use strict;          use Test::More tests => 3;     use Test::WWW::Mechanize;          my $agent = Test::WWW::Mechanize->new;  $agent-  >  get_ok( 'http://search.cpan.org/' );  $agent->field( 'query', 'gerbil counting' );     $agent->submit_form( form_name => 'f' );  $agent-  >  follow_link_ok( { text =  >  'Home', n =  >  '1' } );  $agent->field( 'query', 'test' );     $agent->submit_form( form_name => 'f' );  $agent-  >  follow_link_ok( { text =  >  'Next  >>  ', n =  >  '1' } );  

Running the modified mech2.t should produce three passing tests.

To turn the HTTP::Recorder output into tests, the code instantiates $agent as a Test::WWW::Mechanize object. Note that statements that work as tests have changed. When defining $agent , the test file doesn't need autocheck = > 1 any more because it uses get_ok( ) and follow_link_ok( ) to test the success of a request. follow_link_ok( ) expects a hash reference of arguments just as follow_link( ) does.



Perl Testing. A Developer's Notebook
Perl Testing: A Developers Notebook
ISBN: 0596100922
EAN: 2147483647
Year: 2003
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net