tiistai 20. joulukuuta 2011

Running Selenium tests on PHPUnit

Lately I have been dealing a lot with PHPUnit testing of variety of applications. Main focus has always been on testing the backend classes for applications, but lately I have learned the importance of testing also functional aspect of a web page.

For years I have generated functional test scripts with Firefox's add-on called Selenium IDE, which allows you to record page clicks and text inputs and then run the recording as many times as you wish.

Now, to make full potential of Selenium, one should really take advantage of running Selenium test scripts in PHPUnit to combine both backend testing as well as functional testing. First you need to install Selenium RC server which is used to launch a web browser of your choice and do the things you recorded.

Requirements:

- PHP (obviously)
- PHPUnit
- PHPUnit_Selenium (an extension for PHPUnit)
- Selenium RC server
- Internet browser (Firefox/Chrome/Internet Explorer)

Let's start off with installing PHPUnit from PEAR if you have not done so yet.

$ sudo pear upgrade pear
$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover components.ez.no
$ sudo pear channel-discover pear.symfony-project.com
$ sudo pear install --alldeps phpunit/PHPUnit

Now we need to make sure we install also the Selenium extension to PHPUnit.

$ sudo pear install --force phpunit/PHPUnit_Selenium

In the above command line, we used --force switch to ignore a possible error message about Curl not being installed.  Selenium extension requires that Curl is installed so make sure it is.

Now that we have installed all required PHP classes and extensions, it is time to move on to the Selenium RC server which we connect to each time we run Selenium tests in PHPUnit.

First, browse to http://code.google.com/p/selenium/downloads/list click on selenium-server-standlone-2.*.jar (at the time of writing) link, and then copy the url of the selenium-server-standlone-2.*.jar file.

Next, open up a terminal window and type:

$ sudo su
$ mkdir /usr/lib/selenium/
$ cd /usr/lib/selenium/
$ wget url-you-copied-above
$ mkdir -p /var/log/selenium/
$ chmod a+w /var/log/selenium/

Next, take this attachment, and save it as /etc/init.d/selenium

Now edit the file, and change the filename of the standalone server file (currently selenium-server-standalone-2.0a5.jar) to the name of the server file you downloaded above.

Finally, make the script executable:

$ chmod 755 /etc/init.d/selenium

To start Selenium RC server:

$ /etc/init.d/selenium start

Finally, add the script so that it starts automatically when the server does:

$ update-rc.d selenium defaults
Congratulations, you have now done all the preliminary configurations to get the testing environment up and running.

Ei kommentteja:

Lähetä kommentti