Skip to content

Instantly share code, notes, and snippets.

@gkemmey
Created February 7, 2017 17:52
Show Gist options
  • Select an option

  • Save gkemmey/d6ac3a6eeb4888d7edf290d798be37f7 to your computer and use it in GitHub Desktop.

Select an option

Save gkemmey/d6ac3a6eeb4888d7edf290d798be37f7 to your computer and use it in GitHub Desktop.
This is a mixin that configures a test to use Capybara and Selenium. You can include the `JavascriptDriver` module in your `test_helper.rb`, and then include it in any test files that need to execute Javascript code.
module JavascriptDriver
def self.included(base)
base.class_eval do
include Capybara::DSL # make the Capybara DSL available in all javascript tests
# we can't have our tests runnning in separate fixutres or selenium (capybara) won't have
# access to the data created in the test and vice versa
self.use_transactional_fixtures = false
end
# use a driver that can handle javascript
base.setup { Capybara.current_driver = :selenium }
# make sure our driver change isn't lasting
base.teardown { Capybara.use_default_driver }
# reset session information between tests
base.teardown { Capybara.reset_sessions! }
base.setup do
DatabaseCleaner[:active_record, { connection: :test }].strategy = :truncation
DatabaseCleaner[:active_record, { connection: :test }].start
end
base.teardown do
DatabaseCleaner[:active_record, { connection: :test }].clean
end
end
# not strictly necessary, but i find this a convenient helper method to go ahead and include
def sign_in_with_capybara(user)
visit signin_path
fill_in "session_email", with: user.email
fill_in "session_password", with: user.password
click_button "Sign in"
end
end
# -------- usage --------
require 'test_helper'
class JavascriptTest < ActionDispatch::IntegrationTest
include JavascriptDriver
# test all the things
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment