Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Browsers and Transactions

diabolo edited this page Aug 13, 2010 · 16 revisions

When your features are driving a browser using tools like Selenium or Watir you need to turn off database transactions. This is because your browser and cucumber are running in separate processes. This means that your browser is running against a web server that is using a different database connection than cucumber. Since there are two different connections, if transactions are on, the web server’s connection can’t see the data modified by the cucumber connection as this will not be committed until the transaction completes. With transactions on, data is never committed to the database (instead its rolled back at the end of each scenario). Therefore, the web server’s connection will never see data from cucumber, and your browser won’t either.

If you’re using Ruby on Rails it’s easy to turn of transactions for a feature or particular scenarios (if you’re on 0.3.103 or above), just use the @no-txn tag, e.g.

@no-txn
Feature: Lots of scenarios with transactions off.

or

Feature: ...
  @no-txn
  Scenario: One scenario with transactions off.

Cleaning Your Database

Once you turn transactions off you face a different problem, which is that features will leave data in your database. If you’re using Ruby on Rails, a good tool to deal with this is Ben Mabey’s Database Cleaner gem which you can install with

gem install bmabey-database_cleaner --source http://gems.github.com/

You can use this very effectively with the @no-txn tag. Something like the following infeatures/support/db_cleaner.rb should work well:

require 'database_cleaner'
DatabaseCleaner.clean_with :truncation # clean once to ensure clean slate

Before('@no-txn') do
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.start
end

After('@no-txn') do
  DatabaseCleaner.clean
  DatabaseCleaner.strategy = :transaction
end

If you’re not using Rails, writing something similar should be fairly easy.

Clone this wiki locally