Permalink
Browse files

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.
  • Loading branch information...
eileencodes committed Nov 12, 2016
1 parent 1db7a5c commit 84f82f0a84de0906d195b529a9f780141c43507a
@@ -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
# <tt>ActionSystemTestCase</tt>. System tests use Capybara as a base and
# allows you to configure the driver. The default driver is
# <tt>RailsSeleniumDriver</tt> 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
# <tt>RailsSeleniumDriver</tt> 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 @@
# 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 @@
# +:selenium+, +:webkit+, or +:poltergeist+. These 4 drivers use Capyara's
# driver defaults whereas the <tt>RailsSeleniumDriver</tt> 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
@@ -9,15 +9,11 @@ module DriverAdapter
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
@@ -15,7 +15,6 @@ module TestHelper # :nodoc:
end
def after_teardown
take_screenshot if supported?
Capybara.reset_sessions!
super
end
@@ -18,11 +18,11 @@ def take_screenshot
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))
@@ -53,7 +53,6 @@ module Generators
force_plural: false,
helper: true,
integration_tool: nil,
system_tool: nil,
javascripts: true,
javascript_engine: :js,
orm: false,
@@ -63,6 +62,7 @@ module Generators
stylesheets: true,
stylesheet_engine: :css,
scaffold_stylesheet: true,
system_tests: nil,
test_framework: false,
template_engine: :erb
}
@@ -152,6 +152,8 @@ def test
def system_test
empty_directory_with_keep_file "test/system"
template "test/system_test_helper.rb"
end
def tmp
@@ -0,0 +1,8 @@
require "test_helper"
class ActionSystemTestCase < ActionSystemTest::Base
teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end
@@ -0,0 +1,8 @@
require "test_helper"
class ActionSystemTestCase < ActionSystemTest::Base
teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end
@@ -6,6 +6,7 @@ class ScaffoldGenerator < ResourceGenerator # :nodoc:
remove_hook_for :resource_controller
remove_class_option :actions
class_option :api, type: :boolean
class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets"
class_option :stylesheet_engine, desc: "Engine for Stylesheets"
class_option :assets, type: :boolean
@@ -15,11 +16,12 @@ class ScaffoldGenerator < ResourceGenerator # :nodoc:
def handle_skip
@options = @options.merge(stylesheets: false) unless options[:assets]
@options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] && options[:scaffold_stylesheet]
@options = @options.merge(system_tests: false) if options[:api]
end
hook_for :scaffold_controller, required: true
hook_for :system_tool, as: :system
hook_for :system_tests, as: :system
hook_for :assets do |assets|
invoke assets, [controller_name]
@@ -1,7 +1,7 @@
module Rails
module Generators
class SystemTestGenerator < NamedBase # :nodoc:
hook_for :system_tool, as: :system
hook_for :system_tests, as: :system
end
end
end
@@ -6,7 +6,11 @@ class SystemGenerator < Base # :nodoc:
check_class_collision suffix: "Test"
def create_test_files
template "system_test.rb", File.join("test/system", class_path, "#{file_name.pluralize}_test.rb")
if !File.exist?(File.join("test/system_test_helper.rb"))
template "system_test_helper.rb", File.join("test", "system_test_helper.rb")
end
template "system_test.rb", File.join("test/system", "#{file_name.pluralize}_test.rb")
end
end
end
@@ -1,4 +1,4 @@
require 'test_helper'
require "system_test_helper"
class <%= class_name.pluralize %>Test < ActionSystemTestCase
# test 'the truth' do
@@ -0,0 +1,8 @@
require "test_helper"
class ActionSystemTestCase < ActionSystemTest::Base
teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end
@@ -46,7 +46,7 @@ def before_setup # :nodoc:
end
end
class ActionSystemTestCase
class ActionSystemTest::Base
def before_setup # :nodoc:
@routes = Rails.application.routes
super
@@ -11,7 +11,7 @@ class TestUnitRailtie < Rails::Railtie
fixture_replacement: nil
c.integration_tool :test_unit
c.system_tool :test_unit
c.system_tests :test_unit
end
initializer "test_unit.line_filtering" do
@@ -20,18 +20,6 @@ class TestUnitRailtie < Rails::Railtie
}
end
config.system_testing = ActiveSupport::OrderedOptions.new
initializer "system_testing.set_configs" do |app|
ActiveSupport.on_load(:active_support_test_case) do
require "action_system_test"
options = app.config.system_testing
options.driver ||= ActionSystemTest.default_driver
options.each { |k, v| ActionSystemTest.send("#{k}=", v) }
end
end
rake_tasks do
load "rails/test_unit/testing.rake"
end

0 comments on commit 84f82f0

Please sign in to comment.