Permalink
Browse files

Add ability to run system tests via Capybara

Capybara defaults to Rack Test for it's driver and works out of the box
but this adds the headers and allows for future configurable adapters
for system testing.
  • Loading branch information...
eileencodes committed Aug 5, 2016
1 parent 97d8b7a commit 0862cf1bbffe0a3c82e311804244a8cb715332a6
@@ -1,5 +1,8 @@
+require 'system_testing/base'
+
module Rails
class SystemTestCase < ActiveSupport::TestCase
include Rails.application.routes.url_helpers
+ include SystemTesting::Base
end
end
@@ -0,0 +1,9 @@
+require 'system_testing/test_helper'
+require 'system_testing/driver_adapter'
+
+module SystemTesting
+ module Base
+ include TestHelper
+ include DriverAdapter
+ end
+end
@@ -0,0 +1,18 @@
+require 'system_testing/driver_adapters'
+
+module SystemTesting
+ module DriverAdapter
+ extend ActiveSupport::Concern
+
+ included do
+ self.driver_adapter = :capybara_rack_test_driver
+ end
+
+ module ClassMethods
+ def driver_adapter=(driver_name_or_class)
+ driver = DriverAdapters.lookup(driver_name_or_class).new
+ driver.call
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module SystemTesting
+ module DriverAdapters
+ extend ActiveSupport::Autoload
+
+ autoload :CapybaraRackTestDriver
+
+ class << self
+ def lookup(name)
+ const_get(name.to_s.camelize)
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module SystemTesting
+ module DriverAdapters
+ class CapybaraRackTestDriver
+ attr_reader :useragent
+
+ def initialize(useragent: 'Capybara')
+ @useragent = useragent
+ end
+
+ def call
+ registration
+ end
+
+ private
+ def registration
+ Capybara.register_driver :rack_test do |app|
+ Capybara::RackTest::Driver.new(app, headers: {
+ 'HTTP_USER_AGENT' => @useragent
+ })
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,12 @@
+require 'capybara/rails'
+
+module SystemTesting
+ module TestHelper
+ include Capybara::DSL
+
+ def after_teardown
+ Capybara.reset_sessions!
+ super
+ end
+ end
+end

0 comments on commit 0862cf1

Please sign in to comment.