2

I have the following Cucumber feature testing an input form using typeahead.js:

@javascript
Scenario: Creating a battery using typeahead
  When I create a new battery using typeahead
  Then I should be on the show battery page
  And I should see the battery created message

The test fails on the second step with the following error message:

ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound)
./features/step_definitions/admin/car_part_steps/battery_steps.rb:37:in `/^I should be on the show battery page$/'
features/admin/creating_car_parts/creating_batteries.feature:20:in `Then I should be on the show battery page'

The relevant step definitions are as follows:

When /^I create a new battery using typeahead$/ do
  select_from_typeahead :field => 'battery_manufacturer_typeahead',
    :select => @manufacturer.name
  fill_in 'Type', :with => '700W'
  click_button 'Create Battery'
end

Then /^I should be on the show battery page$/ do
  battery = Battery.find_by_type_and_manufacturer_id!('700W', @manufacturer.id)
  current_path.should == admin_battery_path(battery)
  page.should have_content(battery.type)
end

The select_from_typeahead function is as follows:

def select_from_typeahead(params)
  params[:js_field] ||= params[:field]
  params[:select_typeahead] ||= params[:select]
  fill_in params[:field], :with => params[:select][0, 2]
  page.execute_script "$('##{params[:js_field]}').trigger('focus')"
  page.execute_script "$('##{params[:js_field]}').trigger('keydown')"
  sleep 0.5
  page.execute_script "$('.tt-suggestion:contains(\"#{params[:select_typeahead]}\")').trigger('mouseenter').trigger('click')"
end

The problem appears not to have anything to do with the typeahead itself however, as the code works in the browser, and if I add some debug output, I notice that the battery gets saved to the database in the first step when running the test as well, it just mysteriously disappears before the second step runs.

I think it's an issue with database_cleaner, as I know that doesn't play nice with Javascript when set to use transactions, but I've already tried setting it to use truncation instead and disabled transactional fixtures and it still doesn't work.

My features/support/env.rb currently looks like this:

require 'simplecov'
SimpleCov.start 'rails'

require 'cucumber/rails'

Capybara.default_selector = :css
Capybara.javascript_driver = :webkit

ActionController::Base.allow_rescue = false

Cucumber::Rails::World.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation

Cucumber::Rails::Database.javascript_strategy = :truncation

My environment is as follows:

rails 4.0.2
cucumber 1.3.10
cucumber-rails 1.4.0
capybara 2.2.0
capybara-webkit 1.1.0
database_cleaner 1.2.0

Am I missing something, is there some other way database_cleaner might still interfere with my test, or is it something else entirely that I haven't thought of?

Any ideas would be very welcome!

Tyler
  • 11,272
  • 9
  • 65
  • 105
Baito Aru
  • 21
  • 1

1 Answers1

0

I don't think it has to do with Database Cleaner. That wouldn't do cleanup until the end of your scenario. To debug, I highly recommend installing Capybara Screenshot so that you can see exactly what is going on in your page. It provides you the method screenshot_and_open_image, which you can use to pop open an image of what the browser looks like at that exact moment. My guesses are that:

  1. Your jQuery isn't doing what you expect because of timing issues - have you tried longer pauses?
  2. Your transaction isn't committed. Have you looked in test.log to see if the transaction was committed?
  3. Is your form failing validation? Try putting a screenshot_and_open_image at the beginning of your I should be on the show battery page step to see. Another thing I use is a shared step like this, so that I can add screenshots in my scenarios for debugging:

And /^I take a screenshot$/ do
  Capybara::Screenshot.screenshot_and_open_image
end
Dan Caddigan
  • 1,578
  • 14
  • 25