From 84f82f0a84de0906d195b529a9f780141c43507a Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 12 Nov 2016 09:49:28 -0500 Subject: Refactor config settings to use generated file Originally I had set up system testing to have one configuration option to be set in the test environment. After thinking it over I think a generated class on app creation would be best. The reason for this is Capybara has a ton of configuration options that I'm sure some folks want to use. Thinking about how we handle screenshots, database transactions, and a whole bunch of other settings it would be better for users to be able to turn all of that on and off. When an app or scaffold is generated a `test/system_test_helper.rb` test helper will be generated as well. This will contain the class for tests to inherit from `ActionSystemTestCase` which will inherit from `ActionSystemTest::Base`. Here is where users can change the test driver, remove the screenshot helper, and add their additional Capybara configuration. --- actionsystemtest/lib/action_system_test.rb | 46 +++++++++++++--------- .../lib/action_system_test/driver_adapter.rb | 6 +-- .../lib/action_system_test/test_helper.rb | 1 - .../test_helpers/screenshot_helper.rb | 8 ++-- 4 files changed, 33 insertions(+), 28 deletions(-) (limited to 'actionsystemtest/lib') diff --git a/actionsystemtest/lib/action_system_test.rb b/actionsystemtest/lib/action_system_test.rb index 18b992a3ca..f9fb8b4e5c 100644 --- a/actionsystemtest/lib/action_system_test.rb +++ b/actionsystemtest/lib/action_system_test.rb @@ -1,20 +1,16 @@ -require "action_system_test/test_helper" -require "action_system_test/driver_adapter" - # System tests are similar to Integration tests in that they incorporate multiple # controllers and actions, but can be used to simulate a real user experience. # System tests are also known as Acceptance tests. # -# To create a System Test in your application extend your test class from +# To create a System Test in your application, extend your test class from # ActionSystemTestCase. System tests use Capybara as a base and -# allows you to configure the driver. The default driver is -# RailsSeleniumDriver which provides Capybara with no-setup -# configuration of the Selenium Driver. If you prefer you can use the bare -# Selenium driver and set your own configuration. +# allow you to configure the driver. The default driver is +# RailsSeleniumDriver which provides a Capybara and the Selenium +# Driver with no configuration. It's intended to work out of the box. # # A system test looks like the following: # -# require 'test_helper' +# require 'system_test_helper' # # class Users::CreateTest < ActionSystemTestCase # def adding_a_new_user @@ -28,17 +24,23 @@ require "action_system_test/driver_adapter" # end # end # -# System test driver can be configured in your Rails configuration file for the -# test environment. +# When generating an application or scaffold a +system_test_helper.rb+ will also +# be generated containing the base class for system testing. This is where you can +# change the driver, add Capybara settings, and other configuration for your system +# tests. # -# config.system_testing.driver = :rails_selenium_driver +# class ActionSystemTestCase < ActionSystemTest::Base +# ActionSystemTest.driver = :rack_test +# end # # You can also specify a driver by initializing a new driver object. This allows # you to change the default settings for the driver you're setting. # -# config.system_testing.driver = ActionSystemTest::DriverAdapters::RailsSeleniumDriver.new( -# browser: :firefox -# ) +# class ActionSystemTestCase < ActionSystemTest::Base +# ActionSystemTest.driver = ActionSystemTest::DriverAdapters::RailsSeleniumDriver.new( +# browser: :firefox +# ) +# end # # A list of supported adapters can be found in DriverAdapters. # @@ -47,11 +49,19 @@ require "action_system_test/driver_adapter" # +:selenium+, +:webkit+, or +:poltergeist+. These 4 drivers use Capyara's # driver defaults whereas the RailsSeleniumDriver has pre-set # configuration for browser, server, port, etc. + +require "action_system_test/test_helper" +require "action_system_test/driver_adapter" + module ActionSystemTest include ActionSystemTest::TestHelper include ActionSystemTest::DriverAdapter -end -class ActionSystemTestCase < ActionDispatch::IntegrationTest - include ActionSystemTest + DEFAULT_DRIVER = :rails_selenium_driver + + class Base < ActionDispatch::IntegrationTest + include ActionSystemTest + + ActionSystemTest.driver = DEFAULT_DRIVER + end end diff --git a/actionsystemtest/lib/action_system_test/driver_adapter.rb b/actionsystemtest/lib/action_system_test/driver_adapter.rb index fe12ecaf80..892edd2d32 100644 --- a/actionsystemtest/lib/action_system_test/driver_adapter.rb +++ b/actionsystemtest/lib/action_system_test/driver_adapter.rb @@ -9,15 +9,11 @@ module ActionSystemTest extend ActiveSupport::Concern module ClassMethods - def default_driver # :nodoc - :rails_selenium_driver - end - # Returns the current driver that is set. If no driver is set in the # Rails' configuration file then +:rails_selenium_driver+ will be # initialized. def driver - @driver ||= DriverAdapters.lookup(default_driver) + @driver ||= DriverAdapters.lookup(DEFAULT_DRIVER) end # Specify the adapter and settings for the system test driver set in the diff --git a/actionsystemtest/lib/action_system_test/test_helper.rb b/actionsystemtest/lib/action_system_test/test_helper.rb index 428f0faeca..c9389b83ec 100644 --- a/actionsystemtest/lib/action_system_test/test_helper.rb +++ b/actionsystemtest/lib/action_system_test/test_helper.rb @@ -15,7 +15,6 @@ module ActionSystemTest end def after_teardown - take_screenshot if supported? Capybara.reset_sessions! super end diff --git a/actionsystemtest/lib/action_system_test/test_helpers/screenshot_helper.rb b/actionsystemtest/lib/action_system_test/test_helpers/screenshot_helper.rb index 37336e976f..3264ddc86e 100644 --- a/actionsystemtest/lib/action_system_test/test_helpers/screenshot_helper.rb +++ b/actionsystemtest/lib/action_system_test/test_helpers/screenshot_helper.rb @@ -18,11 +18,11 @@ module ActionSystemTest puts find_image end - private - def supported? - ActionSystemTest.driver.supports_screenshots? && !passed? - end + def take_failed_screenshot + take_screenshot unless passed? + end + private def image_path path = "tmp/screenshots/failures_#{method_name}.png" page.save_screenshot(Rails.root.join(path)) -- cgit v1.2.3