Friday, June 5, 2009

Automate Selenium HTML Tests with Ant

I am a huge fan and new convert to Selenium (http://seleniumhq.org). If you are not sure what it is then click on the link and watch the introductory video. If you don't get it after watching the video then don't bother reading on. Just keep testing your apps manually and repeatedly.

The Selenium IDE allows you to record actions in Firefox. These are saved as HTML tests. I have read other articles about automating the tests by first converting them to other formats such as Java. However I love the ease of maintaining test suites via Selenium IDE. If I want to modify a test case or add something to it - this is very easy. If I want to see where or how a particular test case is failing - this is very easy. However if I want to automate these tests by converting them to Java the process of conversion means an extra step for me. And this would be repeated each time I want to modify a test case. I understand you can make the test cases richer if you convert to Java and do things you cannot do with HTML. But I have not run into a case for this yet. Maybe I am understanding it wrong - please correct me if so.

So in order to make life a bit easier I automated the HTML test cases using Selenium RC and Ant. And because I am using Ant this had the added benefit of being able to initialize my database before the tests are run using a DBUnit task (http://www.dbunit.org/).

Firstly I downloaded Selenium RC and extracted selenium-server.jar.

Secondly I modified my build.xml. Make sure the location points to where you saved the selenium-server.jar.










Thirdly I added a few extra tasks to my build.xml. The selenium_run_suite task will start the selenium server. All you need to do once finished is shut it down. An added benefit is that you can specify a location for results of the test suite.

A gotcha that I ran into was that for testing IE it seems you need to use browser="*iehta" and for Firefox browser="*chrome".










suite="selenium\MyFunctionalTestSuite.html"
browser="*chrome"
results="${reports.dir}/selenium/results.html"
multiWindow="true"
timeoutInSeconds="900"
startURL="http://localhost:8456/" />



dest="result.txt" ignoreerrors="true" />






Thanks to Selenium-RC and Continuous Integration on OpenQA where I knicked the selenium_stop_server task.