Reincarnation

Lolita Technology

Configure capybara-webkit to Run Acceptance Specs With Javascript/AJAX

  • Add capybara-webkit to your Gemfile and let Guard::Bundler install it automatically (or manually via bundle install if you don’t use Guard).
Gemfile
1
gem 'capybara-webkit', '>= 1.0.0.beta4'
  • Set Javascript driver to :webkit for Capybara in spec_helper.rb.
spec_helper.rb
1
Capybara.javascript_driver = :webkit
  • Configure RSpec use non-transactional fixtures, configure Database Cleaner in spec_helper.rb.

    Notice with this setup, we’ll only use truncation strategy when driver is not :rack_test. this will make normal specs run faster.

spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
config.use_transactional_fixtures = false

config.before :each do
  if Capybara.current_driver == :rack_test
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.start
  else
    DatabaseCleaner.strategy = :truncation
  end
end

config.after :each do
  DatabaseCleaner.clean
end
  • Tag your scenarios in spec/acceptance/*_spec.rb to use Javascript driver if necessary.
some_spec.rb
1
2
scenario 'Create a lolita via AJAX', :js => true do
end
  • Wait for any AJAX call to be completed in your specs. This is very important, or you will get many strange issues like no database record found, AJAX call get empty response with 0 status code, etc.

    For example if you have a simple AJAX form, the success callback will simply redirect browser to another page via location.href = '/yet_another_page';. You can use the following code to wait for it done.

another_spec.rb
1
2
3
4
5
6
7
8
9
scenario 'Create a lolita via AJAX', :js => true do
  visit new_lolita_path

  click_on 'Submit'

  wait_until { page.current_path == lolita_path(Lolita.last) }

  # Your expections for the new page
end