Monthly Archives: August 2014

Automated testing with Selenium & PHPUnit

I am working on a much larger project right now that is using Selenium to do unit testing, and found this installation procedure to be a PITA on CentOS so I thought I would share my experience with the interwebs to relieve some pain for others.  I am very new to using these tools so if anyone has suggestions for improvement I am all ears, though this post will be strictly on the install portion.   Expect a much longer and more interesting post in the future.

This is not intended to be a primier on what Selenium is at all.  If you are interested check out this video, docs are located here for PHPUnit, here for Selenium,  and here for Composer (to cover all my reference linkage).

 

This worked as of August 18, using Centos 6.4, JRE 7u67, Selenium 2.42.2, PHP 5.3.3, PHPUnit 4.1.6


 

First, install java if you need it.  I am using the super awesome alternatives command to manage the symlinks of multiple versions.

Get from Sun: jre-7u67-linux-x64.rpm
alternatives –install /usr/bin/java java /usr/java/jre1.7.0_67/bin/java 1
alternatives –config java
pick the new one
At the time of this post, this was a version that worked for me
# java -version
java version “1.7.0_67”
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

Now we want to install Selenium

ln -s /usr/local/bin/selenium-server-standalone-2.42.2.jar /usr/local/bin/selenium-server-standalone.jar 

add to .bash_profile, then relogin or source .bash_profile
alias selenium=”java -jar /usr/local/bin/selenium-server-standalone-2.42.2.jar”

Install packages for X and Firefox, since I’m running this in a headless VM
yum install -y java firefox Xorg xorg-x11-server-Xvfb

There are multiple options to install PHPUnit.   Pear is the easiest as it was what I was familar with but appears to be going end of life at the end of 2014.  Composer is brand new to me – you kids and your fancy package management systems!!!!1 – but looks pretty useful.

(1) via PEAR (end of life at end of 2014)
yum install php php-pear php-xml php-devel -y
pear channel-update pear.php.net
pear config-set auto_discover 1
pear install phpunit
pear install phpunit/PHPUnit_Selenium

(2) via Composer
yum install php php-xml php-devel php-pdo git -y
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
vi composer.json
Add this content:
{
    “require-dev”: {
        “phpunit/phpunit”: “4.1.*”,
        “phpunit/php-invoker”: “*”,
        “phpunit/dbunit”: “*”,
        “phpunit/phpunit-selenium”: “>=1.2”
    }
}
Then run this:
composer install 

Docs say to add this to your path:
~/.composer/vendor/bin/
But I installed my stuff in a subdir as root, so I also added this to the path also
/root/tmp/vendor/bin 

# phpunit –version
PHPUnit 4.1.6 by Sebastian Bergmann
YAY!

Now to setup an X session.  Many ways to do this, here is a quick and dirty way:

Xvfb :99 -screen 0 800x600x16 &
export DISPLAY=:99
selenium 
Now open a new session to the host.

Now for the actual unit test code.   I’ll share a simple example that checks this blog for something as simple as the TITLE html tag.

<?php
class JAASTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function setUp()
    {
        $this->setBrowser(‘firefox’);
        $this->setBrowserUrl(‘http://www.jaas.co/‘);
    }
    public function testTitle()
    {
        $this->url(‘http://www.jaas.co/‘);
        $this->assertEquals(‘Josh As A Service | www.JaaS.co‘, $this->title());
    }
}
?> 
Save it as  JAASTest.php
Invoke it by:
phpunit JAASTest.php

PHPUnit 4.1.6 by Sebastian Bergmann.
Time: 13.65 seconds, Memory: 5.00Mb
OK (1 test, 1 assertion) 
YAY!! Automated testing by little robot minions. Just what I have always wanted.
There is a ton more you can do with this type of testing which I will be exploring soon now that I have this up and working.  Check back later on in the year for the results of the project!
Tagged , , ,