Cucumber + Selenium + Machinist

Posted by Chris on November 02, 2009

This weekend I worked on setting up a Selenium test for Plus 3 Network using Cucumber. I’ve been using Machinist to create fixtures for my non-Selenium Cucumber scenarios. I wanted to continue using my Machinist blueprints that I had already setup so that my Selenium tests would use the same random data.

The issue I ran into was that ActiveRecord doesn’t actaully commit it’s changes to the database, it executes a rollback at the end of each scenario. This is an issue because Webrat/Selenium actaully creates a Mongrel instance and runs it’s tests against that which is a totally different transaction.

The workaround is quite simple, just turn off the transactions for your Selenium environment, use DatabaseCleaner to clean the database and generate your Machinist fixtures in a Before hook. You just need to set Cucumber::Rails::World.use_transactional_fixtures to false in your enhanced.rb and true in your plain.rb. For generating my Machinist fixtures I have a method called “machine_fixtures” (pretty catchy eh?) in my blueprints.rb file. Here is what it looks like:

And my enhanced.rb has the hooks for generating the fixtures using “machine_fixtures” and for cleaning the database.

And for good measure here is what the plain.rb looks like:

I’m assuming you setup your Cucumber/Selenium environment like this: http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium. The instructions mention the whole transaction issue and using DatabaseCleaner to clean out the database but it didn’t quite hit me till I tried using the Machinist fixtures. Hopefully this will clear things up for some other Noob.