HTTP::Recorder and WWW::Mechanize for testing web sites

HTTP::Recorder and WWW::Mechanize for testing web sites

am 18.04.2008 18:33:29 von mikaelb

Hi,
WWW::Mechanize is a Perl module that simulates a web browser.
You can use HTTP::Recorder to write WWW::Mechanize code automatically as
you surf, see the article:
http://www.perl.com/pub/a/2004/06/04/recorder.html

Then you can improve this code with variables and loops and use it for
testing of web sites.

I saved the proxy server program in the article and started it:
../proxy.pl &

I reconfigured Firefox to use the proxy server in
Preferences/Advanced/Network/Connection Settings. It's on port 8080 on
localhost, and use this for all protocols. Remember to remove localhost
and 127.0.0.1 from 'No Proxy for' if you test against a local web server.

Then I test my web application using a newly reseted test database.

After that I stop the proxy:
kill -15 %1

And reset the database.

I then edit the file with output from the proxy script and add at top:
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
# use Test::More qw(no_plan);
my $agent = WWW::Mechanize->new();
$agent->cookie_jar(HTTP::Cookies->new);

I run this program. This should run without errors.

I can also check my web application and see that it is in the state it
should be.

Now I have a test script that I can use after major changes to the web
application to test that everything is still working.

Hidden form fields are considered read-only by HTTP::Recorder and
WWW::Mechanize. I sometimes change them from JavaScript. I solved this
by making them type="text" and setting the style="display:none".

There is a bug in 0.05 of HTTP::Recorder that bothered me but it can be
fixed, see:
http://cpanratings.perl.org/dist/HTTP-Recorder


Regards,
// Mikael