Up to now there was no PHP support for Selenium Remote Control, i.e. you couldn’t integrate your Selenium tests with a testing framework like SimpleTest. On PEAR there is now a package called Testing_Selenium which makes it easy to use PHP and Selenium Remote Control together. To use them you need PHP5 and a JRE (Java Runtime Environment) version 1.5 or higher. The installation itself is simple. Here the installation instructions for CakePHP:

  • Download the package
  • Put Selenium.php to your “vendors” folder
  • Create a folder “Testing” in your “vendors” folder
  • Put the “Selenium” folder from the archive to the “Testing” folder
  • Put selenium-server.jar to a folder of your choice

Ok, as the installation is done, let’s write a simple testcase. We want to verify that the title of the cake website is correct.

vendor('Selenium');

class SeleniumTest extends UnitTestCase
{
    function setUp()
    {
        $this->selenium = new Testing_Selenium("*firefox /usr/lib/firefox/firefox-bin", "http://cakephp.org");
        $result = $this->selenium->start();
    }

    function tearDown()
    {
        $this->selenium->stop();
    }

    function testCakePHPTitle()
    {
        $this->selenium->open("/");
        $this->assertEqual('CakePHP : the rapid development php framework', $this->selenium->getTitle());
    }
}

What does this do? In the setUp() function we start a Firefox browser (we could also use a different browser). With that browser we open in the test function the cake homepage and get the page title. And in tearDown() we close the browser.

Before we can run this test, we have to start the Selenium server with:

java -jar selenium-server.jar

Now, we should get a green bar when we run the test.

Happy testing :)

Update (2006-11-29): If you don’t want to put the exception class to the vendors folder, you can replace in Selenium.php

require_once 'Testing/Selenium/Exception.php';

with

class Testing_Selenium_Exception extends Exception {}