Using Selenium RC to automate your web based testing

31 December, 2007 – 12:53 pm

Selenium is an awesome free alternative to apps such as Quick Test Pro and the like, providing you with a browser based automation suite for web applications.

Selenium uses JavaScript and Iframes to embed a test automation engine in your browser. This technique should work with any JavaScript-enabled browser. Selenium Remote Control provides a Selenium Server, which can automatically start/stop/control any supported browser. It works by using Selenium Core, a pure-HTML+JS library that performs automated tasks in JavaScript.

In this demo, I’ve used Selenium-RC and Perl to automate the checking of a promotional website called FreeStuffDay where every month you can have the chance to secure an advertised item for free. Instead of me sitting behind a browser checking the availability of free items manually, I’ve written a quick demo script to show you how it’s done from Perl and Selenium-RC.

In order to build the script using a record-play method like other mainstream apps, you can download and run Selenium IDE, which is compatible with major browsers. You can export various formats from the Selenium IDE into a portable script. Make the necessary tweaks to your script including things like assertions and page load times. The main logic of my script will log in to the site, check for a free car, and if it doesn’t find one, then loops indefinitely. It also checks page load times. If it finds a winner, then I’d add some extra code to SMS or email me that the session is a winner …

Download video …

My finished code looked something like this … You can see how easy it is to write a fairly simple and robust web automation script, that actually uses a browser to drive your code, which results in a *more* realistic simulation of a typical user …
#!/usr/bin/perl
# #########################################################################
# FreeStuffDay Selenium Script
# $Revision: 0.2 $
# $Date: 2007/12/31 12:26:43 $
# $Author: koops $
# #########################################################################
 
use strict;
use warnings;
use Time::HiRes qw(gettimeofday tv_interval);
use Test::WWW::Selenium;
use WWW::Selenium::Util qw(server_is_running);
use Test::More;
use Test::Exception;
 
my ($host, $port) = server_is_running();
if ($host and $port) {
    plan tests => 1;
}
else {
    plan skip_all => "No selenium server found!";
    exit 0;
}
 
# Start up a new browser session on our Selenium RC server
my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444,
                                    browser => "*firefox",
                                    browser_url => "http://www.freestuffday.com.au" );
 
# Open the main page
$sel->open("/");
print "Loading main page ...\n";
$sel->pause(1000);
 
# Sign in with your credentials
$sel->click("link=or SIGN IN NOW!");
$sel->wait_for_page_to_load("20000");
$sel->type("txtEmailAddress", "user\@email.address.com");
$sel->type("txtPassword", "*******");
$sel->click("btnLogin");
$sel->wait_for_page_to_load("20000");
print "Successful login!\n";
 
# Check for freebies, in this case I'm after the Toyota Yaris
$sel->click("ctl00_phFreestuffDayMaster_reptCompanyTile_ctl00_imgTile");
$sel->wait_for_page_to_load("20000");
$sel->click("ctl00_phFreestuffDayMaster_dlProducts_ctl00_lnkProductName");
$sel->wait_for_page_to_load("20000");
print "Average page load response times (seconds)\n";
 
# The following method checks if the selected product submit button is enabled ...
my $buttonEnabled=$sel->is_editable("ctl00\$phFreestuffDayMaster\$cmdSelectProduct");
 
# If the button is not enabled, then loop repeatedly using the breadcrumb
# navigation to switch between pages automatically and re-check if it is enabled ...
while(!$buttonEnabled){
  my $t0 = [gettimeofday];
  $sel->click("//a[2]/font");
  $sel->wait_for_page_to_load("20000");
  $sel->click("ctl00_phFreestuffDayMaster_dlProducts_ctl00_lnkProductName");
  $sel->wait_for_page_to_load("20000");
  $buttonEnabled=$sel->is_editable("ctl00\$phFreestuffDayMaster\$cmdSelectProduct");
  # While I'm at it, check the response time of this business event
  my $t1 = [gettimeofday];
    my $t0_t1 = tv_interval $t0, $t1;
    my $elapsed = tv_interval ($t0);
  # Print out the average response time ...
    print "\t$elapsed\n";
}
 
# If the script falls out of the while loop on enable, make sure we click the submit button
$sel->click_ok("ctl00\$phFreestuffDayMaster\$cmdSelectProduct");
$sel->wait_for_page_to_load("30000");
 
# Finally, insert some additional code to send an email or SMS notifying we have a winner!

Share it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb

Post a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image