October 15, 2011

The joys of development with Selenium web-testing

October 15, 2011 - Categories: development, geek

I’ve started using the Selenium web-testing framework as part of regular development, and I like it. Selenium makes it easy to automate testing web applications, but it’s also useful for developing web applications.

I was working on improving the administrative interface of a Drupal site. To test the new features, I needed to switch back and forth between different users, and I needed to move nodes through a workflow. Masquerade made it a little bit easier to switch between user roles without logging out and logging in, but there was still a lot of clicking and waiting involved. Selenium made it easy to record and tweak the steps I took, and I could even add assertions so that I didn’t have to check things myself.

Timestamps helped me doublecheck that new data was successfully submitted, and that my e-mail messages weren’t using the old data. Here’s how to store a timestamp:

storeEval { var stamp = new Date(); var year = stamp.getYear() + 1900; var month = stamp.getMonth() + 1; var day = stamp.getDate(); var hours = stamp.getHours(); var mins = stamp.getMinutes(); var time; var secs = stamp.getSeconds(); if (month < 10) { month = "0" + month; } if (day < 10) { day = "0" + day; } if (mins < 10) { mins = "0" + mins; } if (secs < 10) { secs = "0" + secs; } timestamp = "" + year + "-" + month + "-" + day + " " + hours + ":" + mins + ":" + secs; } timestamp

Then I can refer to that timestamp when filling out forms, like this:

type Node title ${timestamp}

I can check if the timestamp is present on a page:

assertTextPresent Node title ${timestamp}

I thought I would get impatient with the slow pace of web testing compared to directly testing the underlying models using code (Simpletest rocks for this). The visual feedback from watching my browser step through the tests helped me appreciate the time I saved compared to clicking through things myself. (And I had extra time to write notes in Emacs or comment my code while the tests are running!)

The Stored Variables Viewer (http://seleniumhq.org/download/) plugin was great for viewing timestamps, saved HTML snippets, and other things stored during test cases. In a pinch, it’s a decent way to explore something in the Selenium IDE context – store a variable, then view it.

Selenium IDE is a free Firefox plugin that you can get at http://seleniumhq.org/download/ Good stuff!