How to use Selenium with SimpleTest

Published on and tagged with cakephp  pear  php  selenium  simpletest  testing

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 {}

9 comments baked

  • Felix Geisendörfer

    Very nice! Now I’m just wondering, why does the Selenium driver need php5, that’s a bit of a bummer.

  • cakebaker

    @Felix: The authors use the “private” and “public” keywords…

  • turtletime

    Cool!

    Does this work on remote dev servers, or does it need to be local?

    I just get confused when you say “it loads firefox.” On the server?

  • cakebaker

    @turtletime: I tested it on my local machine, but it should also work on remote servers. And yes, it loads firefox on the machine where the tests run, or, more precisely, where the selenium server runs. On http://wiki.openqa.org/display/SRC/Selenium-RC+and+Continuous+Integration it is explained how to use it in a continous integration system.

  • cakebaker » Preview of a new Selenium test suite

    […] 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 ;-) […]

  • Chandrapal singh

    Hi ;
    I am using selenium in my application to test web application.

    If I need value of level then I pass Id of level it gives the value of that lavel. same for textbox and same for dropdown.It works Great. But if I want to get the value of 233
    then I am getting the problem because in selenium there is no option to get value of DIV
    Please suggest some thing.
    Thanks
    CP

  • cakebaker

    @Chandrapal: Hm, I don’t understand what you try to do. What do you mean with “get the value of 233”?

  • baka123

    Hi selenium folks ..

    i want to test an application’s login module.

    Scenario:The username and passwords have to be validated.Its like give the username and password ,the system should login-in to the application.Now again i have to log-in into the application with the next username and passwd…This has to continue until all the values in the database are checked …Please do the need full help ..i was success full in loggin a single user ..is there any way of connecting to a database and get values as inputs to selenium…

  • cakebaker

    @baka123: Well, if you use CakePHP you could use something like:

    $db = ConnectionManager::getDataSource('test-db');
    $users = $db->execute('SELECT * FROM users');
    

    And then loop through the result set and perform the tests for the respective user.

    HTH

© daniel hofstetter. Licensed under a Creative Commons License