Some time ago I wrote about using Selenium with SimpleTest. It is a nice solution, but after working with it I had to experience that it doesn’t fit to my working style. I often forgot to start the Selenium server before running the tests, and as it is rather slow, it is not the ideal tool for doing TDD ;-)

So I had to go back to the Selenium helper. But somehow it felt no longer right to place the tests in app/views/pages/tests and to write them as thtml files. And so I wrote the Selenium test suite, which uses a different approach as you will see.

Let’s have a look at a simple example. Before we can start with the example, we have to install the test suite (please be aware that the test suite only works with the not yet released CakePHP 1.2):

  • Download the Zip file from the download section
  • Unpack the Zip file
  • Add the following route to app/config/routes.php:
    Router::connect('/selenium/*', array('controller' => 'selenium', 'action' => 'display'));

With that we are ready to start with the example. As the tests are organized in test suites, we start with such a test suite:

// app/tests/selenium/my_test_suite.php
class MyTestSuite extends SeleniumTestSuite {
    var $title = 'My tests';

    function execute() {
        $this->addTestCase('Login', 'cases/LoginTest');
    }
}

Each file placed in app/tests/selenium is considered to be a test suite, and must extend the class SeleniumTestSuite and implement the execute() function. This function is very simple: you just add the tests to the test suite.

The next step is to create our test case. A test case must extend SeleniumTestCase and, as a test suite, implement the execute() function. This function contains the test logic.

// app/tests/selenium/cases/login_test.php
class LoginTest extends SeleniumTestCase {
    var $title = 'Login';
    var $fixtures = array('Users');

    function setUp() {
        echo 'setUp';
    }

    function tearDown() {
        echo 'tearDown';
    }

    function execute() {
        $this->open('http://myproject.localhost');
        $this->type('name=data[User][username]‘, $this->dho['username']);
        $this->type(’name=data[User][password]‘, $this->dho['password']);
        $this->clickAndWait(’submit’);
        $this->verifyLocation(’http://myproject.localhost/projects’);
    }
}

You probably noticed the $fixtures array. It allows you to write some records to the database before the test case is executed. These records are also available in the test case (see $this->dho). Each fixture is a simple class with the same name as the database table:

// app/tests/fixtures/users.php
class Users {
    var $columns = array('id', 'username', 'password');
    var $dho = array(1, 'dho', 'geheim');
}

With that we are ready to execute the test suite. As I use the Selenium IDE, I just have to enter the following url in Firefox to load the test suite:

chrome://selenium-ide/content/selenium/TestRunner.html?test=http://myproject.localhost/selenium/MyTestSuite

And the tests can be executed :)

Please keep in mind that it is just a preview, so it is possible that some things will change in future versions. As always your feedback is welcome :)