Skip to content

Instantly share code, notes, and snippets.

@radavis
Forked from jacksy40/instructions.md
Last active August 29, 2018 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radavis/e4d1e1c976185a5d9673 to your computer and use it in GitHub Desktop.
Save radavis/e4d1e1c976185a5d9673 to your computer and use it in GitHub Desktop.
Capybara/RSpec/Poltergeist. How to run javascript in your Rails test suite.

install phantomjs

brew install phantomjs

Gemfile:

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

spec/support/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.rb

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