Selenium, phpunit & Drupal

Posted by Lucas Hedding on September 15, 2012
Development
Support

Overview

I was on a project recently where I wrote tests using Selenium for a Drupal site. I chose Selenium over Drupal's SimpleTest to achieve greater cross-browser support. To get started, you'll need to install PHPUnit and PHPUnit_Selenium. For my example, you'll also want to install Jenkins customized for PHPUnit and Apache Ant. Of course, don't forget to download Selenium IDE for Firefoxselenium-server-standalone-2.x.x.jar and the PHP Formatter for Selenium IDE.

I should explain why I opted to go with pure PHPUnit's implementation of Selenium. It seems to have the greatest support and documentation. And our QA tester can build the initial script using the Selenium IDE and export it for use with PHPUnit. This means we can't use the cool features of JsonWireProtocol just yet, but there we should some time soon.

Write the scripts

I wrote a base class that includes some init functions. These are pretty universal to testing the Drupal site and allows me to develop my Unit tests with the assumption that they can run in any order. It resets cookies in the browser and sets the start of a test to a known location, the homepage.

<?php

abstract class BaseTest extends PHPUnit_Extensions_SeleniumTestCase {

  protected $autoStop = FALSE;

  protected function setUp() {
    $this->setBrowserUrl('http://www.example.com');
  }
  /** 
   * common commands required at the start of each test. These cannot be run in the setup 
   * as they require the browser to be opened 
   */
  protected function init() {
    $this->clearAllCookies();
    $this->openHomePage();
  }
  /**
   * Clears all cookies
   */
  private function clearAllCookies() {
    $cookies = explode(';', $this->getCookie());
    foreach ($cookies as $cookie) {
      $cookie_parts = explode('=', $cookie);
      $this->deleteCookie($cookie_parts[0], '/');
    }
  }
  /**
   * Open the homepage and verifies we are in fact on the home page
   */
  private function openHomePage() {
    $this->open("/");
    $this->waitForPageToLoad("30000");
    $this->assertEquals("Home", $this->getText("xpath=//h1[@id='page-title']"));
  }
}

 

Do want to keep your Drupal site running smoothly without worrying about maintenance? We can give your site a tune-up or help you keep it up-to-date for the long-term.

Write us about your project, and we’ll get back to you within 48 hours.


Selenium, phpunit & Drupal