Interface testing with WWW::Mechanize

Using HTTP::Recorder, it is easy to generate tests that use WWW::Mechanize to test the functionality of Koha.
In order to use HTTP::Recorder, you will need to do the following:
# Install HTTP::Recorder and WWW::Mechanize: sudo apt-get install libhttp-proxy-perl libhttp-recorder-perl libwww-mechanize-perl libtest-www-mechanize-perl
# Download the proxy script: wget https://raw.github.com/jcamins/release-tools/master/httprecorder.pl
# Run the proxy script: perl httprecorder.pl
# Open your web browser
# Set your web browser to use a HTTP proxy on 127.0.0.1:9000 (adjust the host name if you are running the httprecorder in a vm) Screenshot
# Navigate to your Koha installation, and do whatever it is you were planning on doing, knowing that it is being recorded in a script for later use
# When you want to look at the script you have generated, go to http://http-recorder
# Remove the HTTP proxy when you are done
== Important warnings ==
=== Logging in with Test::WWW::Mechanize ===
Starting in 3.8.x, logins may not be recorded quite correctly by http-recorder. If you have problems, the following four lines will login correctly (http-recorder may miss the line that sets the branch, I am not sure why):
$agent->field('password', $password);
$agent->field('userid', $user);
$agent->field('branch', );
$agent->click_ok(, 'login to staff client');
=== Uploading files ===
Uploading files using post turns out to not work quite right with Test::WWW::Mechanize. Use the following code instead of the seemingly obvious post_ok() alternative:
$agent->post( "$intranet/cgi-bin/koha/tools/upload-file.pl",
 [
 'fileToUpload' => ["$testdir/data/record.mrc"],
 ],
 'Content_Type' => 'form-data',
 );
ok($agent->success, 'uploaded file');
== Examples ==
=== t/config.pl ===
$config = {
 server => 'localhost:1389',
 bind_as => 'login@example.com', # test rewrite on bind
 password => 'password',
 search => {
 base => 'dc=example,dc=com',
 filter => 'uid=login',
 },
 attributes_required => [ qw/
 uid
 / ],
};
=== t/koha/01-remove-test-user.t ===
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 7;
use Test::WWW::Mechanize;
use XML::Simple;
use Data::Dump qw(dump);
my $url = $ENV{INTRANET} || 'http://ffzg.koha-dev.rot13.org:8080';
my $koha_conf = $ENV{KOHA_CONF} || '/etc/koha/sites/ffzg/koha-conf.xml';
my $xml = XMLin( $koha_conf );
diag 'Koha config = ',dump $xml->{config};
ok( $xml->{config}->{useldapserver}, 'useldapserver' );
our $config;
require 't/config.pl';
diag 'test config = ',dump $config;
my $mech = Test::WWW::Mechanize->new;
$mech->get_ok( $url, "intranet $url" );
$mech->submit_form_ok({
 fields => {
 userid => $xml->{config}->{user},
 password => $xml->{config}->{pass},
 },
}, "login $xml->{config}->{user}");
$mech->submit_form_ok({
 form_number => 2,
 fields => {
 member => $config->{bind_as},
 },
}, 'find patron' );
#diag $mech->content;
$mech->follow_link_ok({ url_regex => qr/moremember/ }, 'details' );
my $html = $mech->content();
if ( $html =~ m{(/cgi-bin/koha/members/deletemem\.pl\?member=\d+)}s ) {
 ok( $1, 'found deletemem' );
 $mech->get_ok( $url . $1 );
}
=== t/koha/02-create-test-user.t ===
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 4;
use Test::WWW::Mechanize;
our $config;
require 't/config.pl';
use WWW::Mechanize;
my $mech = Test::WWW::Mechanize->new;
my $save_count = 1;
sub save {
	my $path = '/tmp/login-' . $save_count++ . '.html';
	open(my $fh, '>', $path);
	print $fh @_;
	warn "# save $path ", -s $path, " bytes\n";
}
my $opac_url = $ENV{OPAC_URL} || 'http://site.example.com';
$mech->get_ok( $opac_url, "opac $opac_url" );
save $mech->content;
$mech->follow_link_ok({ url_regex => qr/opac-user/i }, 'login form' );
save $mech->content;
$mech->submit_form_ok({
	form_number => 2,
	fields => {
		userid => $config->{bind_as},
		password => $config->{password},
	},
}, 'login');
save $mech->content;
$mech->follow_link_ok({ url_regex => qr/logout/ }, 'logout' );
Category:Quality_Assurance_Testing