Adding Selenium tests to Drupal7

Here are some problems I faced when adding selenium to a Drupal7 project I have been working on

1-Test cases are not listed.

Description: when installing the latest Selenium & SimpleTest modules, anything except selenium default tests are not listed in the Configuration/Testing

Fast Solution: Use latest Selenium  module with the default SimpleTest module installed with Drupal core.

2-Running the test case  randomly opens new default Drupal installation.

Description: Simple test is configured to clone the database structure and perform the test cases on this new database so you will not find any of your themes/modules enabled, In some cases the test user created will not be able to login.

Typical Solution: enable the themes/modules when setting up the test and run the patch file shipped with Selenium module without editing it.

Another Solution: run the test cases on the same database (a backup of it of course or whatever!), and this can be done with the following steps

a-Extending the DrupalSeleniumWebTestCase class

class DrupalSeleniumCustomTestCase extends DrupalSeleniumWebTestCase {
    public $onOriginal = TRUE;

b-Editing the patch file shipped with selenium module from

+    if ($time_diff >= 0 && $time_diff <= 500 && $hmac == drupal_hmac_base64($check_string, $key)) {
       $test_prefix = $prefix;

to be

+    if ($time_diff >= 0 && $time_diff = 0 && $hmac == drupal_hmac_base64($check_string, $key)) {
       $test_prefix = $prefix;

c-applying this patch before running the test cases

patch -p1 < patch_file_name.patch

d-Override the tearDown function in your class created in step (a), by copying it from the DrupalSeleniumWebTestCase and commenting one line as follows

$tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
	if (empty($tables)) {
		//$this->fail('Failed to find test tables to drop.');

3-Some exceptions might appear on running the test cases from terminal

Description: You might need to integrate your project in some Continous integration framework so your test cases are run automatically by a terminal command, an exception might be thrown if you are using firefox for not being able to load the firefox profile

Solution: This is a permissions problem, You might need to go to /sites/default/files/ and delete the simpletest folder, then run the test cases from terminal directly after that and it will get created with correct permissions.

4-Select option from a select box.

Description: You might need to select an option from a select control in the test case, option->select() didn't work with me, so as select_control->click() then option->click(), didn't work also.
Solution: This code works!

$select_control = $this->driver->getElement(css=select#select-id');

5-The function isElementPresent return true when the element is not present

Solution: you can override it in your class implemented in 1.a as follows

* Check if element presents on the page.
* @param type $locator
* @return boolean
public function isElementPresent($locator) {
    try {
  		$is_element_present = $this->driver->getElement($locator) != NULL;
  	catch (Exception $e) {
  		$is_element_present = false;
  	return $is_element_present;

Please let me know if I missed something, and dont forget to CLEAR THE CACHE