Skip to content

Instantly share code, notes, and snippets.

@jacksy40
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacksy40/dab754d02d0336a6d011 to your computer and use it in GitHub Desktop.
Save jacksy40/dab754d02d0336a6d011 to your computer and use it in GitHub Desktop.
Capybara/RSpec/Poltergeist. How to run javascript in your test suite.

install phantomjs

brew install phantomjs

Gemfile:

group :test do
  gem 'poltergeist'
  gem 'database_cleaner'
end

spec/database_cleaner.rb

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

spec/rails_helper.

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end

Then, add js: true to the test scenarios where you have javascript to test:

scenario 'upvote', js: true do

see what is happening:

save_screenshot("capybara-js.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment