This entry explains how to install and set-up automated tests, using PHPUnit, Selenium and Xvfb on CentOS Linux.
PHPUnit is a unit testing framework. Selenium is a testing framework for web applications, which can record and playback tests, relying on browsers to perform these tasks. Most servers do not have an instance of the X Window System, in order to open a browser. To avoid installing the heavy-weight packages required for it, we can use the X virtual framebuffer, which renders all the graphical operations into memory; thus, reducing the memory footprint.
Below is a quick guide on how to install this suite of tools:
NOTE: These commands require superuser privileges.
- Install Pear, required by PHPUnit:
yum -y install php-pear.noarch
- Prepare Pear for installing PHPUnit:
pear config-set auto_discover 1 pear channel-discover pear.phpunit.de
- Install PHPUnit and PHPUnit_Selenium Packages:
pear channel-update pear.phpunit.de pear install phpunit/PHPUnit_Selenium
# NOTE: Depending on your system's architecture, # the package name might be different. To locate # the right package, perform: # yum search Xvfb yum -y install xorg-x11-server-Xvfb.i686 yum -y install xauth
- Install Firefox, required by the Selenium Server:
# NOTE: Package names might differ, depending on your # system's architecture. yum -y install firefox.i686
wget -O /usr/local/bin/selenium-server-standalone-2.24.1.jar http://selenium.googlecode.com/files/selenium-server-standalone-2.24.1.jar
- Install Java, required by the Selenium Server:
yum -y install java
- Start Xvfb, on virtual display numer 2:
/usr/bin/xvfb-run Xvfb :2 -screen 0 1024x768x16 &
- Start Selenium Server, connecting to display number 2:
DISPLAY=:2 /usr/bin/java -jar /usr/local/bin/selenium-server-standalone-2.24.1.jar -Dwebdriver.firefox.bin="/usr/lib/firefox/firefox"
- Configure your PHPUnit tests, by adding a similar entry to your phpunit.xml file:
- In order to periodically execute tests, configure a cron job similar to this:
0 0 * * * cd /path/to/test/suite && phpunit >> /var/log/phpunit.log
Further reading:
Firefox Selenium IDE (used for recording tests): http://docs.seleniumhq.org/projects/ide/
Convert recorded Selenium tests to PHPUnit: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/
Configure unit tests to dump screenshots on failure: http://www.phpunit.de/manual/current/en/selenium.html