Virtual Spring Cleaning - in which wild modules ambush me

In April, Google announced that Google Chrome was finally supporting headless mode, at least on Linux and Mac OS. Back then, I noted to myself that this might be a good time to revisit my rough prototype of WWW::Mechanize::Chrome. According to Git, I had written a first prototype of it in 2010 which used the old, raw socket protocol. But time has progressed and the protocol now uses Websockets. My original approach used AnyEvent, so I quickly replaced my own approach using AnyEvent::WebSocket::Client, and the HTTP parts with Future::HTTP.

In one afternoon, I was able to talk to Chrome and have it talk back, so now I have yet another task in front of me, implementing WWW::Mechanize::Chrome. But as this is the third incarnation of modules using the WWW::Mechanize API to automate browsers, I found some code that has been copied between the earlier incarnations, and being in a clensing mood, I released them as separate modules onto CPAN.

For testing only, there now is Test::HTTP::LocalServer, which fires up a specialized local HTTP server usable for testing, convenient if you write your own HTTP clients. It mainly deals with the setup of starting an external process and finding the port address it listens on. It offers some convenience methods to generate URLs that respond with an error, redirect or specialized response and otherwise offers a simple HTML form to fill out from the "browser".

use LWP::Simple qw(get);
my $server = Test::HTTP::LocalServer->spawn;

my $html = get $server->url; # Retrieve $server->url

my $html = get $server->error_timeout(5); # gets a timeout after 5 seconds

$server->stop;

The object offers a convenient way to launch the server as well as methods that return URLs to trigger specific behaviour. You can also retrieve the web server logs to ensure identical behaviour of your clients towards the web server between runs.

This module was distributed with the earlier incarnations, mostly because it is a very specialized prerequisite only used for testing the browser functionality. But as it now lives in three separate distributions and WWW::Mechanize contains an earlier copy of it as well, I've released it onto CPAN and made it an explicit testing prerequisite.

Leave a comment

About Max Maischein

user-pic I'm the Treasurer for the Frankfurt Perlmongers e.V. . I have organized Perl events including 7 German Perl Workshops one YAPC::Europe.