Skip to content

Instantly share code, notes, and snippets.

@jayzz55
Forked from eliotsykes/Gemfile
Last active August 29, 2015 14:10
Show Gist options
  • Save jayzz55/0a1de2e83e9f67b5dfb3 to your computer and use it in GitHub Desktop.
Save jayzz55/0a1de2e83e9f67b5dfb3 to your computer and use it in GitHub Desktop.
# Add poltergeist gem to Gemfile, in :test group,
# then run `bundle` to install
group :test do
...
gem 'poltergeist'
...
end

JavaScript Testing with PhantomJS

PhantomJS is required to run JavaScript-dependent specs.

Install PhantomJS on Ubuntu

SSH into your Vagrant Virtual Machine if you're using one, then run the following commands to install PhantomJS:

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install nodejs
npm install -g phantomjs

Install PhantomJS on Mac OS X

Only do this if you're not using Vagrant:

Follow installation instructions for Mac OS X: http://phantomjs.org/download.html

# For any specs that require JS, in a describe block (top-level or nested),
# specify the option `js: true`
describe "Searching widgets", js: true do
it "shows 10 results" do
...
end
end
# 1. Add 'require "capybara/poltergeist" to rails_helper.rb below
# existing 'require "capybara/rspec" line.
# AND
# 2. Set Capybara's JavaScript driver to be :poltergeist
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require "capybara/rails"
require "capybara/rspec"
require "capybara/poltergeist" # Add this line to require poltergeist
# Specs flagged with `js: true` will use Capybara's JS driver. Set
# that JS driver to :poltergeist
Capybara.javascript_driver = :poltergeist
# We want to load config from spec/support/database_cleaner.rb, so ensure this line is uncommented
# in your rails_helper.rb
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
...
RSpec.configure do |config|
...
# IMPORTANT! Ensure that config.use_transactional_fixtures is false
config.use_transactional_fixtures = false
...
end
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# Configure DatabaseCleaner to use truncation strategy with
# JavaScript (js: true) specs.
#
# We want to ensure we're not using transactions as the work they do is
# isolated to one database connection. One database connection is used by
# the specs, and another, separate database connection is
# used by the app server phantomjs interacts with. Truncation, when *not*
# inside a transaction, affects all database connections, so truncation is
# what we need!
# http://www.railsonmaui.com/tips/rails/capybara-phantomjs-poltergeist-rspec-rails-tips.html
# http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment