Selenium testing in Drupal

After attending our local SeleniumCamp (www.seleniumcamp.com) in February this year I found very interesting possibilities for testing web applications. This topic I would like to share my thoughts about how to use Selenium framework to improve test coverage of our drupal sites.

Why do we need anything else when we have simpletest framework?
There is a number of things that can Selenium do (particulary Webdriver) that we can't test with simpletest. Main advantages of Selenium is that it "emulates" the actions of the browser so it can execute javascripts, upload files and do whatever needed. First I would like to give some small introductory to Selenium and then we will go through some code examples of my work of integration of Selenium to simpletest.

But demo first!

About Selenium

There are numerous products that you can find on the website of the Selenium (http://seleniumhq.org/).

Selenium IDE.

This is plugin to Firefox that you can record your actions and then "play" them. WIth this plugin you can also do some asserts and then ensure that test runs properly. I believe this is the fastest way to try selenium and make some profit of it.

Selenium RC.

This is a server (java application) that you can start to run your recorded tests. Scenario is following: download Selenium Server (formerly the Selenium RC Server) from http://seleniumhq.org/download/ and run it (you need to have your java environment ready. "java -jar selenium-server-standalone-2.0b3.jar"). Export your tests recorded with Selenium IDE to PHP format. Install PHPUnit (you can do it via PEAR installer or using "sudo apt-get install phpunit"). There is a lot instructions on internet how to do that. And run your tests phpunit saved-test.php. Personally I liked this was of recording and executings tests very much as they are very easy to create. Also you can run your tests with Selenium RC from different browsers like chrome, internet explorer.
Selenium RC executes all operations via javascript so you have limitations about it. Like you cannot upload file using Selenium RC.
Best documentation about these tests I found on site of PHPUnit http://www.phpunit.de/manual/3.0/en/selenium.html.

Webdriver

This is most edge technology that joined Selenium lately. The idea is that server communicates with browsers directly via theirs drivers and performs native events with browsers. So the idea is that we completely emulate actions performed by user. Webdriver is joined into Selenium Server so you can follow instructions above to start it. There are no oficial library for communication with Webdriver from PHP. The protocol of communication to webdriver server is described on http://code.google.com/p/selenium/wiki/JsonWireProtocol. I have found two libraries that are opensourced: http://code.google.com/p/php-webdriver-bindings/ and https://github.com/chibimagic/WebDriver-PHP. The second library integrates with PHPUnit. Truly to say I haven't to write tests and run them with PHPUnit but there are asserts there from PHPUnit class.

How we can integrate Selenium to Drupal

The main idea is to create possibility to run Selenium tests on simpletest sandbox. In order to do that we should change user agent headers of our requests. That is already possible in Firfox driver for webdriver. Based on that I have started sandbox here http://drupal.org/sandbox/ygerasimov/1131210. Basically I have taken the code from https://github.com/chibimagic/WebDriver-PHP and started adapting it to comply drupal standards and integrate into simpletest framework. At the moment we are already can do a lot of things including testing javascripts and uploading files. There is sandbox of example of seleniumt test http://drupal.org/sandbox/ygerasimov/1131220 where you can see how easy is to create Selenium test for drupal. I hope we would be able to extend possibilities of other browsers and make our tests to run on Chrome, Internet Explorer and other browsers.

The future

I can see Selenium testing very interesting technology that can help us to build our websites. It can be used in different ways. First of all it can be very good for testing our modules. Another point is that with Selenium IDE we can create tests very fast and to have some basic test coverage of the functionality that needs a lot of clickwork. In my case I have used Selenium recorded scenario to install custom drupal profile as part of build process for continuous integration. I am sure everyone finds his own way to use this framework as it adds so much possibilities to test our software and make it better!