Tuesday, September 18, 2012

Perl scripting: Selenium on Debian wheezy

An urgent requirement came up where I needed to create an automatic script to perform data entry on a mass scale for a client. I gladly took to the Perl framework Test::WWW::Mechanize and happily busied myself with the job at hand. Using this great module, I started working on the script - logging in, navigating to the form, filling in details and submitting the form. After a day and a half of work, I finally had a script to read data from a CSV spreadsheet and import it by simulating the actions an actual user would do with a browser, though, of course much faster. Then came the moment of truth - testing the script on some real data - a spreadsheet with several rows of data. After a little fine tuning, bingo! The data suddenly appeared in the web application! I couldn't have been happier!!

Having written one script, there now was a second one to write. I went about as earlier simulating the actions with Test::WWW::Mechanize. All went well till I came upon a mandatory Javascript link without which the job would not be done. It is at this point, I figured I couldn't proceed with Test::WWW::Mechanize, since it does not implement Javascript. I therefore turned to Test::WWW::Selenium, because of it's Javascript support.

Selenium is a software which emulates browser actions. Supporting major browsers including Firefox, Google Chrome and Internet Explorer, it is an excellent tool for Web developers to run automated tests on web applications. So I installed the perl package Test::WWW::Selenium on my Debian Wheezy:
# apt-get install libtest-www-selenium-perl

I then installed the Selenium RC, also called the Selenium standalone server from http://seleniumhq.org/download/. I started the server using the command:
$ java -jar selenium-server-standalone-2.25.0.jar
INFO: Launching a standalone server
12:04:33.046 INFO - Java: Sun Microsystems Inc. 19.1-b02
12:04:33.074 INFO - OS: Linux 2.6.32-5-686 i386
12:04:33.184 INFO - v2.25.0, with Core v2.25.0. Built from revision 17482
12:04:34.105 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
12:04:34.107 INFO - Version Jetty/5.1.x
12:04:34.109 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
12:04:34.111 INFO - Started HttpContext[/selenium-server,/selenium-server]
12:04:34.111 INFO - Started HttpContext[/,/]
12:04:35.619 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@1fae3c6
12:04:35.619 INFO - Started HttpContext[/wd,/wd]
12:04:35.624 INFO - Started SocketListener on 0.0.0.0:4444

I wrote a short and simple test script to visit the Google homepage and executed it:
$ perl sel-test.pl

However, I got some errors at this point:
java.lang.IllegalArgumentException: URI "file:selenium-server.jar" is not hierarchical".
After some searching, I found from a blog article http://tero.tilus.net/rutinat/2009/08/18/cucumber-running-selenium-on-debian-lenny/, that I needed to install sun-java6-jdk which is not available on Wheezy. The alternative is to install it from Oracle website. Instead, I used the alternative openjdk-6-jdk and icedtea-plugin:
# apt-get install openjdk-6-jdk icedtea-plugin

This got rid of the problem, but I ran into another:
... INFO - Preparing Firefox profile...
Could not read application.ini
... ERROR - Failed to start new browser session, shutdown browser and clear all session data
java.lang.RuntimeException: Timed out waiting for profile to be created!

I found that the solution to this was to add the firefox/iceweasel directory to the PATH:
$ export PATH=$PATH:/usr/lib/iceweasel

After doing this, I tried by test script and voila!

No comments: