Archive

Posts Tagged ‘DSL’

Selenium on Rails DSL

January 3rd, 2009

When upgrading Rails to version 2.2.2 on one of the applications I work on, it was clear that the SeleniumOnRails plugin was not compatible. This was easily fixed by upgrading the plugin with the non-official version available at here.

The test cases written in RSelanese use a DSL (Domain Specific Language) module to help keep the test code clean and easy to read.

At the time I set this up I was fairly new to Selenium and SeleniumOnRails, and followed some advice in this article that suggested to hack the the RSelenese class to include a custom module for your DSLs. As a Selenium newbie I was probably focusing on the tests, and forgot to fix the weird hack suggested. Needless to say this came back an bit me when I upgraded SeleniumOnRails and reminded me to fix it. This is how I did it.

Why a DSL?

Let me start with a short intro to what I wanna do.

The application I’m working on uses AJAX to login. To do this in my Selenium test cases I would run the following .rsel snippet:

As several test cases will need to log in before getting down to business, pasting that snippet into the test cases would produce enormous amount of duplication. One way to get rid of the duplicated code would be to put the login steps in a .rsel partial and execute the login that way. The tests would in that case include the partial using the following syntax:

I guess you would agree this is not a very tasteful syntax. I’d rather see a method call like login("quentin", "test"). If we define a login method in a module, we could include that in the RSelenese class and make it available to our test cases.

Including the DSL module in RSelenese

With the DSL module in place it still needs to be included in the class SeleniumOnRails::RSelenese. As this setup is only needed in the test environment, the class_eval is done in the test environment config. It is put in an after_initialize block to allow the SeleniumOnRails plugin to be loaded before the inclusion.

That’s it!
Just remember that you need to restart your environment when you make any updates to your DSL module.

Rails , ,