MojoMojo and Selenium test

As suggested by Mateu I was further looking into MojoMojo Selenium tests on CPAN. Actually all selenium.t tests require a test configuration file mojomojo.yml file resides in t/app/mojomojo.yml this file is used to add the authentication configurations. Also resides in the same directory the sqlite mojomojo database: mojomojo.db.

The complete test is as follows:

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

# This test requires that a Selenium server be running.
# A Selenium server # is included in Alien::SeleniumRC which is a dependency of
# Test::WWW::Selenium::Catalyst so having it installed should be sufficient to run this test.
# # The selenium server is a java application that can also be started like:
# java -jar selenium-server.jar
# See http://seleniumhq.org/ for the download of Selenium Remote Control (RC) # which includes a selenium server.

BEGIN { eval {require Test::WWW::Selenium::Catalyst};
plan skip_all => 'Selenium tests need Test::WWW::Selenium::Catalyst' if $@;
plan skip_all => 'Set SELENIUM_TESTS ENV variable to enable Selenium tests' unless $ENV{SELENIUM_TESTS};
plan tests => 22;
$ENV{MOJOMOJO_CONFIG} = 't/app/mojomojo.yml'; }
Test::WWW::Selenium::Catalyst->import('MojoMojo');
my $sel = Test::WWW::Selenium::Catalyst->start;
$sel->open_ok("/");
$sel->is_text_present_ok("Log in");
$sel->open_ok("admin.profile");
$sel->is_text_present_ok("Log in");
$sel->open_ok(".recent");
$sel->is_text_present_ok("Log in");
$sel->open_ok(".list");
$sel->is_text_present_ok("Log in");
$sel->click_ok("link=Log in");
$sel->wait_for_page_to_load_ok( "15000");
$sel->type_ok( "loginField", "admin" );
$sel->type_ok( "pass", "admin" );
$sel->click_ok("//input[@value='Login']");
$sel->wait_for_page_to_load_ok("15000");
$sel->is_text_present_ok("admin");
# Check that .recent was not cached.
$sel->open_ok(".recent");
$sel->is_text_present_ok("Log out");
# Check that profile was no cached.
$sel->open_ok("admin.profile");
$sel->is_text_present_ok("Log out");
$sel->open_ok(".list");
$sel->is_text_present_ok("Log out");
$sel->click_ok("link=Log out");

Reference: MojoMojo on CPAN

Testing Javascript in Perl applications

There is a tool called Selenium which is used to test the Javascript component in Perl based applications. This is used to run tests directly in the browser. Actually this tool sits between Perl application, test script and browser. The test script sends the Selenium server commands to run inside the web browser. The web browser then executes those commands, and returns the results to Selenium server and then to test script. It requires JRE and a working web server to work with.

Jonathan Rockway in his book has explained and used this in Catalyst applications.

Data Serialization in Perl examples

One can store data and objects into a file using Serialization. The bytes that is generated can then be re-used to generate an object that will be identical (a clone) to the stored object. We can achieve this by using Storable module from CPAN. The example below will show a way to store and retrieve such objects.

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Data::Dumper;
use Storable;

my %hash1 = ('Test1' => '10',
	     'Test2' => '20');

store %hash1, "serialized_file";
print "##### HASH1 #####n";
print Dumper(%hash1);

my %hash2 = %{retrieve "serialized_file"};
print "n##### HASH2 #####n";
print Dumper(%hash2);

exit 0;

Output:

##### HASH1 #####
$VAR1 = {
          'Test2' => '20',
          'Test1' => '10'
        };

##### HASH2 #####
$VAR1 = {
          'Test2' => '20',
          'Test1' => '10'
        };


For more info see perldoc Storable
Serialization is used to store data and objects into a file for instance. The serie of bytes that is generated can then be re-used to generate an object that will be identical (a clone) to the stored object.