Last updated February 6, 2014. Created by sukr_s on February 27, 2010.
Edited by 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 \selenium-server.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/bootstrap.inc';
      drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
      function login($browser){
         //navigate to the login page
         $browser->click("link=Login");
         $browser->waitForPageToLoad(CONST_WAIT_PERIOD);
         //enter the user & password
         $browser->type("edit-name", "myuser");
         $browser->type("edit-pass", "mypassword");
         $browser->click("edit-submit");
         $browser->waitForPageToLoad(CONST_WAIT_PERIOD);
      }
      function logout($browser){
        $browser->click("link=Log out");
      }
      class test  extends PHPUnit_Extensions_SeleniumTestCase{
        function setUp(){
          $this->setBrowser("*chrome");
          $this->setBrowserUrl("http://localhost");
        }
      //All the functions here must start with test* only then the tests are executed
        function testMyTest(){
          $this->open("/");
          login($this);
          module_invoke('my_test_module','testMyTest',$this);
          logout($this);
        }
      }

      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

HAPPY TESTING!

Multisite Tip

Here is a tip for use with multisites: http://drupal.org/node/684734

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