2

I am writing a scenario for signup form.

     @abc

     @selenium

     Scenario:Non registered user signs up
        Given I am on the sign-up page
        When I fill in the following:
          |first_name|Anidhya|
          |last_name|Ahuja|
          |email|anidhya@gmail.com|
          |password|123456|
        And I press "submit"
        Then I should see "Registration complete"

I want to use database cleaner to roll back the test database after this scenario so that I can use this scenario again and again.

For that inside my env.rb file I wrote:

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner.strategy = :transaction

  Cucumber::Rails::World.use_transactional_fixtures = true

rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end 


Before('@abc') do
  DatabaseCleaner.start
end
After('@abc') do
  DatabaseCleaner.clean
end

Now when I run the scenario , the user gets saved in the database and the database cleaner fails. I dont see any error messages

  • Could you please clarify how to use database cleaner for only one scenario.I only want to use cleaner for this scenario.
  • Also could you please also provide the vital difference between using truncation and transaction.I think truncation clears the whole database but I dont want that.
  • Is there a better method of doing signup testing than this?
Anidhya Ahuja
  • 883
  • 7
  • 22
  • "I think truncation clears the whole database but I dont want that" Why not? If this is a test database, why would you want data hanging around from a previous test run? It'll only screw you up later. If you need to create a state, find a way to do it that'll let you create it easily for every single test run. – Srdjan Pejic Sep 22 '11 at 12:19
  • because i want a test for signing in also, which requires that user to remain signed up ,atleast till that step ,rgt?could you please tell if the code written in the env file for database cleaner is correct? – Anidhya Ahuja Sep 22 '11 at 12:39

1 Answers1

5

You can't run transactions with selenium because the test runs on two separate instances of the app AFAIK

oreoshake
  • 4,712
  • 1
  • 31
  • 38
  • that's right. the selenium driver doesn't support transactions, as it uses a separate instance, and several requests. – Marian Theisen Sep 23 '11 at 22:05
  • 1
    you have to truncate your database and reload your fixtures. which makes it slow. pays to have a CI server. – Marian Theisen Sep 23 '11 at 22:06
  • ya...i too just read that you cant use transaction strategy with selenium...so now m trying to use factory girl for fixtures.Do you prefer fixtures or factories? Thank you so much for your reply.:) – Anidhya Ahuja Sep 26 '11 at 05:22
  • If you use the webkit driver, you can tell active to share transactions with the webkit driver and setting some capybara config. it escapes me at the moment so please PM me if you're interested so I can remember to update this – oreoshake Oct 13 '11 at 02:49
  • 2
    [This answer](http://stackoverflow.com/a/5525526/418819) has a hack for using transactional fixtures with Selenium tests. – Steve Jul 17 '12 at 14:30