Last updated 9 September 2014. Created on 27 February 2010.
Edited by dokumori, peter.kehl, Albert Volkman, bekasu. Log in to edit this page.

You can find several articles via Google search on how to use Selenium for testing your module using the Selenium IDE. However if you have written modules like I've where the element ids change with every run then the Selenium IDE approach will not help as it's nearly impossible to maintain the ids across different environment. I've used the following approach that works pretty well.

      Install and run Selenium RC.

    • Download from here
    • copy the selenium-server.jar to you root of PHP installation directory
    • run from command prompt java -jar

      Create the test.php unit

      In the root of Drupal installation, create a test.php. All the tests will be launched from here. In order to make full use of Drupal APIs and capabilities you need to instantiate Drupal here. Add the following code in test.php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

include_once './includes/';

function login($browser){
   //navigate to the login page
   //enter the user & password
   $browser->type("edit-name", "myuser");
   $browser->type("edit-pass", "mypassword");

function logout($browser){
  $browser->click("link=Log out");

class test  extends PHPUnit_Extensions_SeleniumTestCase{
  function setUp(){
//All the functions here must start with test* only then the tests are executed
  function testMyTest(){


    Write your test cases

    Create a test module e.g. my_test_module like any other Drupal module
    I've created on test module that orchestrates all the tests and I don't want to include the tests within the module itself as I do not want this deployed in the productive system. You can choose to do it in the way you like it.
    Implement the test methods that you need.

  function my_test_module_testMyTest($browser){
    //perform your tests and assertions. Note that you have full access to Drupal API like db_query etc.

To quickly get to the UI code that is required for testing, you can use the Selenium IDE and generate the code by recording your actions and later changing the format to PHP. One thing you will quickly notice is that the generated code has $this which refers to the browser instance and this must be available for the test to run. Replace $this with $browser that is passed to the function.
And with this you can run it reliably on any test environment.

    Run the test

    Enable the test module and run the tests by using a new command prompt

C:\<drupalrool>> phpunit test.php

Note that phpunit installed in step 1 must be in the PATH.

    Before I forget, there are couple of things...

  • Clear cache before every test run using
    update_sql("truncate db_prefix.cache");
  • variable_set, get and del do not work as the db reference is not correct, so you can do the following. Also don't forget to delete the variable entry from cache
    update_sql("DELETE FROM db_prefix.variable where name='$var'");	
    update_sql("DELETE FROM db_prefix.cache where cid='variables'");
  • Occasionally the tests fail with the server showing NullPointerException, I’ve not be able to figure out the reason for this, but just running the tests again works


Multisite Tip

Here is a tip for use with multisites:

Looking for support? Visit the forums, or join #drupal-support in IRC.