From 97d8b7abfe75b6a7617966ad0b3d37ae9fc7adb8 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 5 Aug 2016 09:58:43 -0400 Subject: Add skeleton for Rails::SystemTestCase This skelton is the bare minimum to get system tests to actually run in an application. This of course doesn't yet actually run a test but it is enough for `bin/rails test:system` to attempt to run files in `test/system` that inherit from `Rails::SystemTestCase`. --- actionpack/lib/system_test_case.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 actionpack/lib/system_test_case.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb new file mode 100644 index 0000000000..fdaae3ff40 --- /dev/null +++ b/actionpack/lib/system_test_case.rb @@ -0,0 +1,5 @@ +module Rails + class SystemTestCase < ActiveSupport::TestCase + include Rails.application.routes.url_helpers + end +end -- cgit v1.2.3 From 0862cf1bbffe0a3c82e311804244a8cb715332a6 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 5 Aug 2016 10:17:22 -0400 Subject: 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. --- actionpack/lib/system_test_case.rb | 3 +++ actionpack/lib/system_testing/base.rb | 9 ++++++++ actionpack/lib/system_testing/driver_adapter.rb | 18 ++++++++++++++++ actionpack/lib/system_testing/driver_adapters.rb | 13 ++++++++++++ .../driver_adapters/capybara_rack_test_driver.rb | 24 ++++++++++++++++++++++ actionpack/lib/system_testing/test_helper.rb | 12 +++++++++++ 6 files changed, 79 insertions(+) create mode 100644 actionpack/lib/system_testing/base.rb create mode 100644 actionpack/lib/system_testing/driver_adapter.rb create mode 100644 actionpack/lib/system_testing/driver_adapters.rb create mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb create mode 100644 actionpack/lib/system_testing/test_helper.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index fdaae3ff40..4265798073 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -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 diff --git a/actionpack/lib/system_testing/base.rb b/actionpack/lib/system_testing/base.rb new file mode 100644 index 0000000000..ae4236f97d --- /dev/null +++ b/actionpack/lib/system_testing/base.rb @@ -0,0 +1,9 @@ +require 'system_testing/test_helper' +require 'system_testing/driver_adapter' + +module SystemTesting + module Base + include TestHelper + include DriverAdapter + end +end diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb new file mode 100644 index 0000000000..70c4396323 --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -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 diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb new file mode 100644 index 0000000000..38e2bdddcc --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters.rb @@ -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 diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb new file mode 100644 index 0000000000..2890686e29 --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb @@ -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 diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb new file mode 100644 index 0000000000..68b187ed7a --- /dev/null +++ b/actionpack/lib/system_testing/test_helper.rb @@ -0,0 +1,12 @@ +require 'capybara/rails' + +module SystemTesting + module TestHelper + include Capybara::DSL + + def after_teardown + Capybara.reset_sessions! + super + end + end +end -- cgit v1.2.3 From 4f08bc0808ab93f70a029f15ac094d61fc5ba121 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 5 Aug 2016 10:30:07 -0400 Subject: Add configurable selenium driver for capybara This is not yet configurable but is the minimum required to make Capybara work with the Selenium driver. A lot of this will change as the tests get fleshed out and the initialization requirements will eventually be configurable via the application. --- actionpack/lib/system_testing/driver_adapters.rb | 1 + .../driver_adapters/capybara_selenium_driver.rb | 79 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb index 38e2bdddcc..a8933b4496 100644 --- a/actionpack/lib/system_testing/driver_adapters.rb +++ b/actionpack/lib/system_testing/driver_adapters.rb @@ -3,6 +3,7 @@ module SystemTesting extend ActiveSupport::Autoload autoload :CapybaraRackTestDriver + autoload :CapybaraSeleniumDriver class << self def lookup(name) diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb new file mode 100644 index 0000000000..b4cb10da4f --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb @@ -0,0 +1,79 @@ +require 'rack/handler/puma' +require 'selenium-webdriver' + +module SystemTesting + module DriverAdapters + class CapybaraSeleniumDriver + attr_reader :browser, :server, :port, :screen_size + + def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [1400,1400]) + @browser = browser + @server = server + @port = port + @screen_size = screen_size + end + + def call + registration + setup + end + + private + def registration + register_browser_driver + register_server + end + + def setup + set_server + set_driver + set_port + end + + def register_browser_driver + Capybara.register_driver @browser do |app| + Capybara::Selenium::Driver.new(app, browser: @browser).tap do |driver| + driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) + end + end + end + + def register_server + Capybara.register_server @server do |app, port, host| + case @server + when :puma + register_puma(app, port) + when :webrick + register_webrick(app, port, host) + else + register_default(app, port) + end + end + end + + def register_default(app, port) + Capybara.run_default_server(app, port) + end + + def register_puma(app, port) + ::Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') + end + + def register_webrick(app, port, host) + Rack::Handler::WEBrick.run(app, Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0)) + end + + def set_server + Capybara.server = @server + end + + def set_driver + Capybara.default_driver = @browser.to_sym + end + + def set_port + Capybara.server_port = @port + end + end + end +end -- cgit v1.2.3 From 0056c9b977a4cd61334909ff950de9358ec84d74 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 5 Aug 2016 12:02:56 -0400 Subject: Add ForHelper's for system tests These FormHelpers are selectors that aren't a capybara default but are considered useful for Rails applications. --- actionpack/lib/system_testing/test_helper.rb | 2 ++ actionpack/lib/system_testing/test_helpers.rb | 7 +++++ .../lib/system_testing/test_helpers/form_helper.rb | 30 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 actionpack/lib/system_testing/test_helpers.rb create mode 100644 actionpack/lib/system_testing/test_helpers/form_helper.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index 68b187ed7a..9ac2e02f01 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -1,8 +1,10 @@ require 'capybara/rails' +require 'system_testing/test_helpers' module SystemTesting module TestHelper include Capybara::DSL + include TestHelpers::FormHelper def after_teardown Capybara.reset_sessions! diff --git a/actionpack/lib/system_testing/test_helpers.rb b/actionpack/lib/system_testing/test_helpers.rb new file mode 100644 index 0000000000..add9596463 --- /dev/null +++ b/actionpack/lib/system_testing/test_helpers.rb @@ -0,0 +1,7 @@ +module SystemTesting + module TestHelpers + extend ActiveSupport::Autoload + + autoload :FormHelper + end +end diff --git a/actionpack/lib/system_testing/test_helpers/form_helper.rb b/actionpack/lib/system_testing/test_helpers/form_helper.rb new file mode 100644 index 0000000000..6ce5c54864 --- /dev/null +++ b/actionpack/lib/system_testing/test_helpers/form_helper.rb @@ -0,0 +1,30 @@ +module SystemTesting + module TestHelpers + module FormHelper + def fill_in_all_fields(fields) + fields.each do |name, value| + fill_in name, with: value + end + end + + def click_checkbox_label(name, checked: false) + field = find_checkbox(name, checked) + label = find_label_wrapper(field) + label.click + end + + def press_enter + page.driver.browser.action.send_keys(:enter).perform + end + + private + def find_checkbox(name, checked) + find(:field, name, visible: :all, checked: checked) + end + + def find_label_wrapper(field, location: './ancestor::label') + field.find :xpath, location + end + end + end +end -- cgit v1.2.3 From 0dc63281da1c7075ce63d8dba62e4230d72bfc2a Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 7 Aug 2016 09:01:16 -0400 Subject: Add configuration option for driver adapter This allows any application to change the driver adapter based on the config settings in the test env. --- actionpack/lib/system_testing/base.rb | 2 ++ actionpack/lib/system_testing/driver_adapter.rb | 6 ++---- actionpack/lib/system_testing/railtie.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 actionpack/lib/system_testing/railtie.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/base.rb b/actionpack/lib/system_testing/base.rb index ae4236f97d..770342da82 100644 --- a/actionpack/lib/system_testing/base.rb +++ b/actionpack/lib/system_testing/base.rb @@ -5,5 +5,7 @@ module SystemTesting module Base include TestHelper include DriverAdapter + + ActiveSupport.run_load_hooks(:system_testing, self) end end diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index 70c4396323..6d94582395 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -4,11 +4,9 @@ module SystemTesting module DriverAdapter extend ActiveSupport::Concern - included do - self.driver_adapter = :capybara_rack_test_driver - end - module ClassMethods + attr_accessor :driver_adapter + def driver_adapter=(driver_name_or_class) driver = DriverAdapters.lookup(driver_name_or_class).new driver.call diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb new file mode 100644 index 0000000000..aa3df29344 --- /dev/null +++ b/actionpack/lib/system_testing/railtie.rb @@ -0,0 +1,14 @@ +module SystemTesting + class Railtie < Rails::Railtie + config.system_testing = ActiveSupport::OrderedOptions.new + + initializer "system_testing.set_configs" do |app| + options = app.config.system_testing + options.driver_adapter ||= :capybara_rack_test_driver + + ActiveSupport.on_load(:system_testing) do + options.each { |k,v| send("#{k}=", v) } + end + end + end +end -- cgit v1.2.3 From 93eff636a677215eb130f775b99e9421fb30f7a2 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 12 Aug 2016 08:19:55 -0400 Subject: Add test assertion helpers Adds assertions that are not part of Capybara but may be useful to Rails users writing system tests. --- actionpack/lib/system_testing/test_helper.rb | 1 + actionpack/lib/system_testing/test_helpers.rb | 1 + .../lib/system_testing/test_helpers/assertions.rb | 32 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 actionpack/lib/system_testing/test_helpers/assertions.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index 9ac2e02f01..bb68cfe665 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -5,6 +5,7 @@ module SystemTesting module TestHelper include Capybara::DSL include TestHelpers::FormHelper + include TestHelpers::Assertions def after_teardown Capybara.reset_sessions! diff --git a/actionpack/lib/system_testing/test_helpers.rb b/actionpack/lib/system_testing/test_helpers.rb index add9596463..074a45d5c8 100644 --- a/actionpack/lib/system_testing/test_helpers.rb +++ b/actionpack/lib/system_testing/test_helpers.rb @@ -3,5 +3,6 @@ module SystemTesting extend ActiveSupport::Autoload autoload :FormHelper + autoload :Assertions end end diff --git a/actionpack/lib/system_testing/test_helpers/assertions.rb b/actionpack/lib/system_testing/test_helpers/assertions.rb new file mode 100644 index 0000000000..865b5e59db --- /dev/null +++ b/actionpack/lib/system_testing/test_helpers/assertions.rb @@ -0,0 +1,32 @@ +module SystemTesting + module TestHelpers + module Assertions + def assert_all_of_selectors(*items) + options = items.extract_options! + type = type_for_selector(items) + + items.each do |item| + assert_selector type, item, options + end + end + + def assert_none_of_selectors(*items) + options = items.extract_options! + type = type_for_selector(items) + + items.each do |item| + assert_no_selector type, item, options + end + end + + private + def type_for_selector(*items) + if items.first.is_a?(Symbol) + items.shift + else + Capybara.default_selector + end + end + end + end +end -- cgit v1.2.3 From fa2b7b03cc82bb40c76c37c91e4663de3ec0a77f Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 26 Aug 2016 12:24:51 -0400 Subject: Inherit from ActionDispatch::IntegrationTest Integration tests already handle all the fancy url mapping we need to do so inherting from that allows us to not need to reinvent the wheel in terms of loading up the route handling required to use `visit users_path` over `visit /users`. --- actionpack/lib/system_test_case.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index 4265798073..3a621fb6f0 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -1,8 +1,7 @@ require 'system_testing/base' module Rails - class SystemTestCase < ActiveSupport::TestCase - include Rails.application.routes.url_helpers + class SystemTestCase < ActionDispatch::IntegrationTest include SystemTesting::Base end end -- cgit v1.2.3 From 9edc998d2a5dcaa5898c11bef29c97403941cff5 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 27 Aug 2016 10:57:08 -0400 Subject: Refactor driver adapter getter/setter This makes it easier to ask the system test what driver adapter it is currently using, and makes it easier to change that setting when necessary. --- actionpack/lib/system_testing/driver_adapter.rb | 13 +++++++++---- actionpack/lib/system_testing/railtie.rb | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index 6d94582395..3958fa4559 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -5,11 +5,16 @@ module SystemTesting extend ActiveSupport::Concern module ClassMethods - attr_accessor :driver_adapter + def default_driver + :capybara_rack_test_driver + end + + def driver + @driver ||= DriverAdapters.lookup(default_driver).new + end - def driver_adapter=(driver_name_or_class) - driver = DriverAdapters.lookup(driver_name_or_class).new - driver.call + def driver=(adapter: default_driver, settings: {}) + @driver = DriverAdapters.lookup(adapter).new(settings) end end end diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb index aa3df29344..f77184ad3d 100644 --- a/actionpack/lib/system_testing/railtie.rb +++ b/actionpack/lib/system_testing/railtie.rb @@ -4,7 +4,7 @@ module SystemTesting initializer "system_testing.set_configs" do |app| options = app.config.system_testing - options.driver_adapter ||= :capybara_rack_test_driver + options.driver ||= {} ActiveSupport.on_load(:system_testing) do options.each { |k,v| send("#{k}=", v) } -- cgit v1.2.3 From faba2505ddf6d0c2825cddcc0468a89232a9d8ae Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 27 Aug 2016 16:47:24 -0400 Subject: Refactor to not include `capybara/rails` Rails itself is not a Rails application so instead of including `capybara/rails` we should use the code in there to set up the test. The only reason capybara needs to include capybara/rails in the first place is because Rails didn't yet support it. --- actionpack/lib/system_testing/test_helper.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index bb68cfe665..bbf4e469ac 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -1,11 +1,22 @@ -require 'capybara/rails' +require 'capybara/dsl' require 'system_testing/test_helpers' module SystemTesting module TestHelper - include Capybara::DSL include TestHelpers::FormHelper include TestHelpers::Assertions + include Capybara::DSL + + Capybara.app = Rack::Builder.new do + map "/" do + run Rails.application + end + end + + def before_setup + Base.driver.call + super + end def after_teardown Capybara.reset_sessions! -- cgit v1.2.3 From d63cfa2738520392ec37b2f77302a6f9f6df1618 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 17 Sep 2016 09:36:02 -0400 Subject: Move SystemTesting::Base into SystemTestCase There's no real benefit to the using the `Base` class here because `SystemTestCase` is already a very small class. --- actionpack/lib/system_test_case.rb | 8 ++++++-- actionpack/lib/system_testing/base.rb | 11 ----------- 2 files changed, 6 insertions(+), 13 deletions(-) delete mode 100644 actionpack/lib/system_testing/base.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index 3a621fb6f0..2072a88d22 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -1,7 +1,11 @@ -require 'system_testing/base' +require 'system_testing/test_helper' +require 'system_testing/driver_adapter' module Rails class SystemTestCase < ActionDispatch::IntegrationTest - include SystemTesting::Base + include SystemTesting::TestHelper + include SystemTesting::DriverAdapter + + ActiveSupport.run_load_hooks(:system_testing, self) end end diff --git a/actionpack/lib/system_testing/base.rb b/actionpack/lib/system_testing/base.rb deleted file mode 100644 index 770342da82..0000000000 --- a/actionpack/lib/system_testing/base.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'system_testing/test_helper' -require 'system_testing/driver_adapter' - -module SystemTesting - module Base - include TestHelper - include DriverAdapter - - ActiveSupport.run_load_hooks(:system_testing, self) - end -end -- cgit v1.2.3 From 4533bb7dcbdd6e0451395bc869d6c3603c6ff1df Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 11 Sep 2016 13:33:52 -0400 Subject: Fix Railtie to pass class when setting options This will clean up the railtie quite a bit, rather than passing a set of hash keys call the new class directly like we do with ActiveJob. Only call driver once when tests start rather than in every single test setup. This is more performant, and the other way was creating unnecessary calls. --- actionpack/lib/system_testing/driver_adapter.rb | 11 +++++++++-- actionpack/lib/system_testing/railtie.rb | 4 +++- actionpack/lib/system_testing/test_helper.rb | 5 ----- 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index 3958fa4559..47d37a26b7 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -13,8 +13,15 @@ module SystemTesting @driver ||= DriverAdapters.lookup(default_driver).new end - def driver=(adapter: default_driver, settings: {}) - @driver = DriverAdapters.lookup(adapter).new(settings) + def driver=(adapter) + @driver = case adapter + when Symbol + DriverAdapters.lookup(adapter).new + else + adapter + end + + @driver.call end end end diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb index f77184ad3d..6c1ddf1448 100644 --- a/actionpack/lib/system_testing/railtie.rb +++ b/actionpack/lib/system_testing/railtie.rb @@ -1,10 +1,12 @@ +require 'system_test_case' + module SystemTesting class Railtie < Rails::Railtie config.system_testing = ActiveSupport::OrderedOptions.new initializer "system_testing.set_configs" do |app| options = app.config.system_testing - options.driver ||= {} + options.driver ||= Rails::SystemTestCase.default_driver ActiveSupport.on_load(:system_testing) do options.each { |k,v| send("#{k}=", v) } diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index bbf4e469ac..9f2778919c 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -13,11 +13,6 @@ module SystemTesting end end - def before_setup - Base.driver.call - super - end - def after_teardown Capybara.reset_sessions! super -- cgit v1.2.3 From 9730b1dba6d1bb9684a54915ac3735d9c0eade26 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 27 Aug 2016 16:48:24 -0400 Subject: Add tests for system testing * Adds test case test * Adds driver adapter test * Adds tests for capybara seleium driver (testing the settings not actually opening the browser to test capybara w/ selenium because that would so so so slow) * Adds tests for rack test driver * Adds tests for generators --- actionpack/actionpack.gemspec | 1 + actionpack/test/abstract_unit.rb | 1 + .../capybara_rack_test_driver_test.rb | 26 ++++++++++ .../capybara_selenium_driver_test.rb | 58 ++++++++++++++++++++++ .../test/system_testing/driver_adapter_test.rb | 19 +++++++ 5 files changed, 105 insertions(+) create mode 100644 actionpack/test/system_testing/capybara_rack_test_driver_test.rb create mode 100644 actionpack/test/system_testing/capybara_selenium_driver_test.rb create mode 100644 actionpack/test/system_testing/driver_adapter_test.rb (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 2c24a54305..bb9079dc1c 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -26,6 +26,7 @@ Gem::Specification.new do |s| s.add_dependency "rails-html-sanitizer", "~> 1.0", ">= 1.0.2" s.add_dependency "rails-dom-testing", "~> 2.0" s.add_dependency "actionview", version + s.add_dependency "capybara", "~> 2.7.0" s.add_development_dependency "activemodel", version end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 459b0d6c54..3b35af6d3a 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -33,6 +33,7 @@ require "action_view/testing/resolvers" require "action_dispatch" require "active_support/dependencies" require "active_model" +require "system_test_case" require "pp" # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late diff --git a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb new file mode 100644 index 0000000000..0f037760d3 --- /dev/null +++ b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb @@ -0,0 +1,26 @@ +require 'abstract_unit' + +class CapybaraRackTestDriverTest < ActiveSupport::TestCase + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::CapybaraRackTestDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal 'Capybara', Rails::SystemTestCase.driver.useragent + end + + def test_setting_useragent + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + useragent: 'x' + ) + assert_equal 'x', Rails::SystemTestCase.driver.useragent + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + made_up_arg: 'x' + ) + end + end +end diff --git a/actionpack/test/system_testing/capybara_selenium_driver_test.rb b/actionpack/test/system_testing/capybara_selenium_driver_test.rb new file mode 100644 index 0000000000..6357b35aa6 --- /dev/null +++ b/actionpack/test/system_testing/capybara_selenium_driver_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +class CapybaraSeleniumDriverTest < ActiveSupport::TestCase + def setup + Rails::SystemTestCase.driver = :capybara_selenium_driver + end + + def test_setting_driver_adapter_to_selenium + assert_kind_of SystemTesting::DriverAdapters::CapybaraSeleniumDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :chrome, Rails::SystemTestCase.driver.browser + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size + end + + def test_setting_browser + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + browser: :firefox + ) + + assert_equal :firefox, Rails::SystemTestCase.driver.browser + end + + def test_setting_server + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + server: :webrick + ) + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port + end + + def test_setting_screen_size + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + screen_size: [ 800, 800 ] + ) + + assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + made_up_arg: 'x' + ) + end + end +end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb new file mode 100644 index 0000000000..bfbeaa63a2 --- /dev/null +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' + +class DriverAdapterTest < ActiveSupport::TestCase + test 'only registered adapters are accepted' do + assert_raises(NameError) do + Rails::SystemTestCase.driver = :whatever + end + + assert_nothing_raised do + Rails::SystemTestCase.driver = :capybara_selenium_driver + end + end + + test 'settings can only be used for the appropriate adapter' do + assert_raises(ArgumentError) do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new(something: 'test') + end + end +end -- cgit v1.2.3 From b44320254167152383b1fa8792cb17847a51fb49 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 11 Sep 2016 17:00:26 -0400 Subject: Add documentation for system tests * Document Rails::SystemTestCase * Document setting drivers with the configration options * Document using the getter/setter for driver adapters * Document the CapybaraRackTestDriver and defaults * Document the CapybaraSeleniumDriver and defaults * Document custom assertions provided by System Testing * Document custom form helpers provided by System Testing --- actionpack/lib/system_test_case.rb | 45 +++++++++++++++++++ actionpack/lib/system_testing/driver_adapter.rb | 11 ++++- actionpack/lib/system_testing/driver_adapters.rb | 10 +++++ .../driver_adapters/capybara_rack_test_driver.rb | 23 +++++++++- .../driver_adapters/capybara_selenium_driver.rb | 50 +++++++++++++++++++++- actionpack/lib/system_testing/railtie.rb | 3 +- actionpack/lib/system_testing/test_helper.rb | 2 +- .../lib/system_testing/test_helpers/assertions.rb | 8 ++++ .../lib/system_testing/test_helpers/form_helper.rb | 28 ++++++++++++ 9 files changed, 173 insertions(+), 7 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index 2072a88d22..be3858bba8 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -2,6 +2,51 @@ require 'system_testing/test_helper' require 'system_testing/driver_adapter' module Rails + # System tests are similar to Integration tests in that they incorporate multiple + # controllers and actions, but can be used to similate 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 + # Rails::SystemTestCase. System tests use Capybara as a base and + # allows you to configure the driver. The default driver is RackTest. + # + # require 'test_helper' + # + # class Users::CreateTest < Rails::SystemTestCase + # def adding_a_new_user + # visit users_path + # click_on 'New User' + # + # fill_in 'Name', with: 'Arya' + # click_on 'Create User' + # + # assert_text 'Arya' + # end + # end + # + # System tests in your application can be configured to use different drivers. + # + # To specify a driver, add the following to your Rails' configuration file for + # the test environment. + # + # config.system_testing.driver = :capybara_selenium_driver + # + # You can also specify a driver with a new driver object. Through this method + # you can also change the default settings for the driver you're setting. + # + # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + # useragent: 'My Useragent' + # ) + # + # A list of supported adapters can be found in DriverAdapters. + # + # If you want to use a driver that is not supported by Rails but is available + # in Capybara, you can override Rails settings and use Capybara directly by + # setting the +Capybara.default_driver+ and +Capybara.javascript_driver+ in + # your test_help file. + # + # You can also skip using Rails system tests completely by not inheriting from + # Rails::SystemTestCase and following Capybara's instructions. class SystemTestCase < ActionDispatch::IntegrationTest include SystemTesting::TestHelper include SystemTesting::DriverAdapter diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index 47d37a26b7..cf44a4fd6b 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -1,18 +1,27 @@ require 'system_testing/driver_adapters' module SystemTesting + # The SystemTesting::DriverAdapter module is used to load the driver + # set in your Rails' test configuration file. + # + # The default driver adapters is the +:capybara_rack_test_driver+. module DriverAdapter extend ActiveSupport::Concern module ClassMethods - def default_driver + def default_driver # :nodoc :capybara_rack_test_driver end + # Returns the current driver that is set. If no driver is set in the + # Rails' configuration file then +:capybara_rack_test_driver+ will be + # initialized. def driver @driver ||= DriverAdapters.lookup(default_driver).new end + # Specify the adapter and settings for the system test driver set in the + # Rails' configuration file. def driver=(adapter) @driver = case adapter when Symbol diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb index a8933b4496..d0771f89cb 100644 --- a/actionpack/lib/system_testing/driver_adapters.rb +++ b/actionpack/lib/system_testing/driver_adapters.rb @@ -1,4 +1,10 @@ module SystemTesting + # == System Testing Driver Adapters + # + # System testing supports the following drivers: + # + # * {RackTest}[https://github.com/brynary/rack-test] + # * {Selenium}[https://github.com/SeleniumHQ/selenium] module DriverAdapters extend ActiveSupport::Autoload @@ -6,6 +12,10 @@ module SystemTesting autoload :CapybaraSeleniumDriver class << self + # Returns driver for specified name. + # + # SystemTesting::DriverAdapters.lookup(:capybara_selenium_driver) + # # => SystemTesting::DriverAdapters::CapybaraSeleniumDriver def lookup(name) const_get(name.to_s.camelize) end diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb index 2890686e29..78abfa05ee 100644 --- a/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb @@ -1,13 +1,32 @@ module SystemTesting module DriverAdapters + # == CapybaraRackTestDriver for System Testing + # + # This is the default driver for Capybara. This driver does not support + # JavaScript because it doesn't open a browser when running the test suite. + # + # Although it does not support JavaScript testing the + # CapybaraRackTestDriver is fast and efficient. This driver requires + # no setup and becasue it does not need a webserver, additional configuration + # is not required. + # + # The CapybaraRackTestDriver only takes one argument for initialization + # which is +:useragent+. + # + # To set the useragent add the following to your + # Rails' configuration file: + # + # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + # useragent: 'My UserAgent' + # ) class CapybaraRackTestDriver attr_reader :useragent - def initialize(useragent: 'Capybara') + def initialize(useragent: 'Capybara') # :nodoc: @useragent = useragent end - def call + def call # :nodoc: registration end diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb index b4cb10da4f..5585b3e9f1 100644 --- a/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb @@ -3,17 +3,63 @@ require 'selenium-webdriver' module SystemTesting module DriverAdapters + # == CapybaraSeleniumDriver for System Testing + # + # The CapybaraSeleniumDriver uses the Selenium 2.0 webdriver. The + # selenium-webdriver gem is required by this driver. + # + # The CapybaraSeleniumDriver is useful for real browser testing and + # support Chrome and Firefox. + # + # To set your system testing to use the Selenium web driver add the + # following to your Rails' configuration test environment: + # + # config.system_testing.driver = :capybara_selenium_driver + # + # Because this driver supports real browser testing it is required that a + # server is configured. + # + # If no server is specified when the driver is initialized, Puma will be used + # by default. The default settings for the CapybaraSeleniumDriver + # are: + # + # # + # + # The settings for the CapybaraSeleniumDriver can be changed from + # Rails' configuration file. + # + # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + # server: :webkit, + # port: 28100, + # screen_size: [ 800, 800 ] + # ) + # + # The default browser is set to chrome because the current version of + # Firefox does not work with selenium-webdriver. If you want to use Firefox, + # you will need to use Firefox 45.0esr or 47.0 and ensure + # that selenium-webdriver is version 2.53.4. To change the browser from + # +:chrome+ to +:firefox+, initialize the selenium driver in your Rails' + # test environment: + # + # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + # browser: :firefox + # ) class CapybaraSeleniumDriver attr_reader :browser, :server, :port, :screen_size - def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [1400,1400]) + def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [1400,1400]) # :nodoc: @browser = browser @server = server @port = port @screen_size = screen_size end - def call + def call # :nodoc: registration setup end diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb index 6c1ddf1448..459a8e9623 100644 --- a/actionpack/lib/system_testing/railtie.rb +++ b/actionpack/lib/system_testing/railtie.rb @@ -1,7 +1,8 @@ require 'system_test_case' module SystemTesting - class Railtie < Rails::Railtie + # = System Testing Railtie + class Railtie < Rails::Railtie # :nodoc: config.system_testing = ActiveSupport::OrderedOptions.new initializer "system_testing.set_configs" do |app| diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index 9f2778919c..2027a55d90 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -2,7 +2,7 @@ require 'capybara/dsl' require 'system_testing/test_helpers' module SystemTesting - module TestHelper + module TestHelper # :nodoc: include TestHelpers::FormHelper include TestHelpers::Assertions include Capybara::DSL diff --git a/actionpack/lib/system_testing/test_helpers/assertions.rb b/actionpack/lib/system_testing/test_helpers/assertions.rb index 865b5e59db..6e5b62e3c5 100644 --- a/actionpack/lib/system_testing/test_helpers/assertions.rb +++ b/actionpack/lib/system_testing/test_helpers/assertions.rb @@ -1,6 +1,11 @@ module SystemTesting module TestHelpers + # Assertions for system testing that aren't included by default in Capybara. + # These are assertions that are useful specifically for Rails applications. module Assertions + # Asserts that all of the provided selectors are present on the given page. + # + # assert_all_of_selectors('p', 'td') def assert_all_of_selectors(*items) options = items.extract_options! type = type_for_selector(items) @@ -10,6 +15,9 @@ module SystemTesting end end + # Asserts that none of the provided selectors are present on the page. + # + # assert_none_of_selectors('ul', 'ol') def assert_none_of_selectors(*items) options = items.extract_options! type = type_for_selector(items) diff --git a/actionpack/lib/system_testing/test_helpers/form_helper.rb b/actionpack/lib/system_testing/test_helpers/form_helper.rb index 6ce5c54864..4789694bbb 100644 --- a/actionpack/lib/system_testing/test_helpers/form_helper.rb +++ b/actionpack/lib/system_testing/test_helpers/form_helper.rb @@ -1,18 +1,46 @@ module SystemTesting module TestHelpers + # Form helpers for system testing that aren't included by default in + # Capybara. module FormHelper + # Finds all provided fields or text areas and fills in with supplied values. + # + # fill_in_all_fields('Name' => 'Eileen', 'Job Title' => 'Programmer') def fill_in_all_fields(fields) fields.each do |name, value| fill_in name, with: value end end + # Locates a checkbox that is present inside a label and checks it. When + # using styled boxes Selenium may not be able to see the checkbox. This + # form helper looks inside the checkbox and clicks the label instead of + # setting the value of the checkbox. + # + # click_checkbox_label 'Admin' + # + # By default +click_checkbox_label+ looks for checkboxes that are not + # checked by default. To locate an already checked box and uncheck it + # set checked to true: + # + # click_checkbox_label 'Admin', checked: true def click_checkbox_label(name, checked: false) field = find_checkbox(name, checked) label = find_label_wrapper(field) label.click end + # In lieu of locating a button and calling +click_on+, +press_enter+ will + # submit the form via enter. This method will only work for drivers that + # load a browser like Selenium. + # + # test 'Adding a User' do + # fill_in 'Name', with: 'Arya' + # + # press_enter + # + # assert_text 'Arya' + # end def press_enter page.driver.browser.action.send_keys(:enter).perform end -- cgit v1.2.3 From f482eddbeff9fe3d0dc6cdaa9a4b53df839f667c Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 1 Oct 2016 11:17:43 -0400 Subject: Reconfigure how the drivers work This removes the useless Rack Test Driver that Rails was providing and moves to a shim like approach for default adapters. If someone wants to use one of the default Capybara Drivers then we will initialize a new `CapybaraDriver` that simply sets the default driver. Rails though is much more opinionated than Capybara and to make system testing a "works out of the box" framework in Rails we have the `RailsSeleniumDriver`. This driver sets defaults that Rails deems important for selenium testing. The purpose of this is to simply add a test and it just works. --- actionpack/lib/system_test_case.rb | 37 +++--- actionpack/lib/system_testing/driver_adapter.rb | 18 +-- actionpack/lib/system_testing/driver_adapters.rb | 40 +++++-- .../driver_adapters/capybara_driver.rb | 57 ++++++++++ .../driver_adapters/capybara_rack_test_driver.rb | 43 ------- .../driver_adapters/capybara_selenium_driver.rb | 125 -------------------- .../driver_adapters/rails_selenium_driver.rb | 126 +++++++++++++++++++++ .../test/system_testing/capybara_driver_test.rb | 10 ++ .../capybara_rack_test_driver_test.rb | 26 ----- .../capybara_selenium_driver_test.rb | 58 ---------- .../test/system_testing/driver_adapter_test.rb | 8 +- .../system_testing/rails_selenium_driver_test.rb | 58 ++++++++++ 12 files changed, 308 insertions(+), 298 deletions(-) create mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_driver.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb create mode 100644 actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb create mode 100644 actionpack/test/system_testing/capybara_driver_test.rb delete mode 100644 actionpack/test/system_testing/capybara_rack_test_driver_test.rb delete mode 100644 actionpack/test/system_testing/capybara_selenium_driver_test.rb create mode 100644 actionpack/test/system_testing/rails_selenium_driver_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index be3858bba8..196798495f 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -3,12 +3,17 @@ require 'system_testing/driver_adapter' module Rails # System tests are similar to Integration tests in that they incorporate multiple - # controllers and actions, but can be used to similate a real user experience. + # 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 # Rails::SystemTestCase. System tests use Capybara as a base and - # allows you to configure the driver. The default driver is RackTest. + # 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. + # + # A system test looks like the following: # # require 'test_helper' # @@ -24,29 +29,25 @@ module Rails # end # end # - # System tests in your application can be configured to use different drivers. - # - # To specify a driver, add the following to your Rails' configuration file for - # the test environment. + # System test driver can be configured in your Rails configuration file for the + # test environment. # - # config.system_testing.driver = :capybara_selenium_driver + # config.system_testing.driver = :rails_selenium_driver # - # You can also specify a driver with a new driver object. Through this method - # you can also change the default settings for the driver you're setting. + # 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 = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - # useragent: 'My Useragent' + # config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + # browser: :firefox # ) # # A list of supported adapters can be found in DriverAdapters. # - # If you want to use a driver that is not supported by Rails but is available - # in Capybara, you can override Rails settings and use Capybara directly by - # setting the +Capybara.default_driver+ and +Capybara.javascript_driver+ in - # your test_help file. - # - # You can also skip using Rails system tests completely by not inheriting from - # Rails::SystemTestCase and following Capybara's instructions. + # If you want to use one of the default drivers provided by Capybara you can + # set the driver in your config to one of those defaults: +:rack_test+, + # +:selenium+, +:webkit+, or +:poltergeist+. These 4 drivers use Capyara's + # driver defaults whereas the RailsSeleniumDriver has pre-set + # configuration for browser, server, port, etc. class SystemTestCase < ActionDispatch::IntegrationTest include SystemTesting::TestHelper include SystemTesting::DriverAdapter diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index cf44a4fd6b..3d8545f3c6 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -4,32 +4,26 @@ module SystemTesting # The SystemTesting::DriverAdapter module is used to load the driver # set in your Rails' test configuration file. # - # The default driver adapters is the +:capybara_rack_test_driver+. + # The default driver adapter is the +:rails_selenium_driver+. module DriverAdapter extend ActiveSupport::Concern module ClassMethods def default_driver # :nodoc - :capybara_rack_test_driver + :rails_selenium_driver end # Returns the current driver that is set. If no driver is set in the - # Rails' configuration file then +:capybara_rack_test_driver+ will be + # Rails' configuration file then +:rails_selenium_driver+ will be # initialized. def driver - @driver ||= DriverAdapters.lookup(default_driver).new + @driver ||= DriverAdapters.lookup(default_driver) end # Specify the adapter and settings for the system test driver set in the # Rails' configuration file. - def driver=(adapter) - @driver = case adapter - when Symbol - DriverAdapters.lookup(adapter).new - else - adapter - end - + def driver=(driver) + @driver = DriverAdapters.lookup(driver) @driver.call end end diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb index d0771f89cb..bb65fe9111 100644 --- a/actionpack/lib/system_testing/driver_adapters.rb +++ b/actionpack/lib/system_testing/driver_adapters.rb @@ -1,23 +1,45 @@ module SystemTesting # == System Testing Driver Adapters # - # System testing supports the following drivers: + # By default Rails supports Capybara with the Selenium Driver. Rails provides + # configuration setup for using the selenium driver with Capybara. + # Additionally Rails can be used as a layer between Capybara and its other + # supported drivers: +:rack_test+, +:selenium+, +:webkit+, or +:poltergeist+. # - # * {RackTest}[https://github.com/brynary/rack-test] - # * {Selenium}[https://github.com/SeleniumHQ/selenium] + # *{RackTest}[https://github.com/jnicklas/capybara#racktest] + # *{Selenium}[http://seleniumhq.org/docs/01_introducing_selenium.html#selenium-2-aka-selenium-webdriver] + # *{Webkit}[https://github.com/thoughtbot/capybara-webkit] + # *{Poltergeist}[https://github.com/teampoltergeist/poltergeist] + # + # === Driver Features + # + # | | Default Browser | Supports Screenshots? | + # |-----------------|-----------------------|-----------------------| + # | Rails' Selenium | Chrome | Yes | + # | Rack Test | No JS Support | No | + # | Selenium | Firefox | Yes | + # | WebKit | Headless w/ Qt | Yes | + # | Poltergeist | Headless w/ PhantomJS | Yes | module DriverAdapters extend ActiveSupport::Autoload - autoload :CapybaraRackTestDriver - autoload :CapybaraSeleniumDriver + autoload :CapybaraDriver + autoload :RailsSeleniumDriver class << self # Returns driver for specified name. # - # SystemTesting::DriverAdapters.lookup(:capybara_selenium_driver) - # # => SystemTesting::DriverAdapters::CapybaraSeleniumDriver - def lookup(name) - const_get(name.to_s.camelize) + # SystemTesting::DriverAdapters.lookup(:rails_selenium_driver) + # # => SystemTesting::DriverAdapters::RailsSeleniumDriver + def lookup(driver) + if CapybaraDriver::CAPYBARA_DEFAULTS.include?(driver) + CapybaraDriver.new(driver) + elsif driver.is_a?(Symbol) + klass = const_get(driver.to_s.camelize) + klass.new + else + driver + end end end end diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb new file mode 100644 index 0000000000..84941a9929 --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb @@ -0,0 +1,57 @@ +module SystemTesting + module DriverAdapters + # == CapybaraDriver for System Testing + # + # The CapybaraDriver is a shim that sits between Rails and + # Capybara. + # + # The drivers Capybara supports are: +:rack_test+, +:selenium+, +:webkit+, + # and +:poltergeist+. + # + # Rails provides its own defaults for Capybara with the Selenium driver + # through RailsSeleniumDriver, but allows users to use Selenium + # directly. + # + # To set your system tests to use one of Capybara's default drivers, add + # the following to yur Rails' configuration test environment: + # + # config.system_testing.driver = :rack_test + # + # The +:rack_test+ driver is a basic test driver that doesn't support + # JavaScript testing and doesn't require a server. + # + # The +:poltergeist+ and +:webkit+ drivers are headless, but require some + # extra environment setup. Because the default server for Rails is Puma, each + # of the Capybara drivers will default to using Puma. Changing the configuration + # to use Webrick is possible by initalizing a new driver object. + # + # The default settings for the CapybaraDriver are: + # + # # + # + # The settings for the CapybaraDriver can be changed from + # Rails' configuration file. + # + # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( + # name: :webkit, + # server: :webrick + # ) + class CapybaraDriver + CAPYBARA_DEFAULTS = [ :rack_test, :selenium, :webkit, :poltergeist ] + + attr_reader :name + + def initialize(name) + @name = name + end + + def call + Capybara.default_driver = @name + end + end + end +end diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb deleted file mode 100644 index 78abfa05ee..0000000000 --- a/actionpack/lib/system_testing/driver_adapters/capybara_rack_test_driver.rb +++ /dev/null @@ -1,43 +0,0 @@ -module SystemTesting - module DriverAdapters - # == CapybaraRackTestDriver for System Testing - # - # This is the default driver for Capybara. This driver does not support - # JavaScript because it doesn't open a browser when running the test suite. - # - # Although it does not support JavaScript testing the - # CapybaraRackTestDriver is fast and efficient. This driver requires - # no setup and becasue it does not need a webserver, additional configuration - # is not required. - # - # The CapybaraRackTestDriver only takes one argument for initialization - # which is +:useragent+. - # - # To set the useragent add the following to your - # Rails' configuration file: - # - # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - # useragent: 'My UserAgent' - # ) - class CapybaraRackTestDriver - attr_reader :useragent - - def initialize(useragent: 'Capybara') # :nodoc: - @useragent = useragent - end - - def call # :nodoc: - 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 diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb deleted file mode 100644 index 5585b3e9f1..0000000000 --- a/actionpack/lib/system_testing/driver_adapters/capybara_selenium_driver.rb +++ /dev/null @@ -1,125 +0,0 @@ -require 'rack/handler/puma' -require 'selenium-webdriver' - -module SystemTesting - module DriverAdapters - # == CapybaraSeleniumDriver for System Testing - # - # The CapybaraSeleniumDriver uses the Selenium 2.0 webdriver. The - # selenium-webdriver gem is required by this driver. - # - # The CapybaraSeleniumDriver is useful for real browser testing and - # support Chrome and Firefox. - # - # To set your system testing to use the Selenium web driver add the - # following to your Rails' configuration test environment: - # - # config.system_testing.driver = :capybara_selenium_driver - # - # Because this driver supports real browser testing it is required that a - # server is configured. - # - # If no server is specified when the driver is initialized, Puma will be used - # by default. The default settings for the CapybaraSeleniumDriver - # are: - # - # # - # - # The settings for the CapybaraSeleniumDriver can be changed from - # Rails' configuration file. - # - # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - # server: :webkit, - # port: 28100, - # screen_size: [ 800, 800 ] - # ) - # - # The default browser is set to chrome because the current version of - # Firefox does not work with selenium-webdriver. If you want to use Firefox, - # you will need to use Firefox 45.0esr or 47.0 and ensure - # that selenium-webdriver is version 2.53.4. To change the browser from - # +:chrome+ to +:firefox+, initialize the selenium driver in your Rails' - # test environment: - # - # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - # browser: :firefox - # ) - class CapybaraSeleniumDriver - attr_reader :browser, :server, :port, :screen_size - - def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [1400,1400]) # :nodoc: - @browser = browser - @server = server - @port = port - @screen_size = screen_size - end - - def call # :nodoc: - registration - setup - end - - private - def registration - register_browser_driver - register_server - end - - def setup - set_server - set_driver - set_port - end - - def register_browser_driver - Capybara.register_driver @browser do |app| - Capybara::Selenium::Driver.new(app, browser: @browser).tap do |driver| - driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) - end - end - end - - def register_server - Capybara.register_server @server do |app, port, host| - case @server - when :puma - register_puma(app, port) - when :webrick - register_webrick(app, port, host) - else - register_default(app, port) - end - end - end - - def register_default(app, port) - Capybara.run_default_server(app, port) - end - - def register_puma(app, port) - ::Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') - end - - def register_webrick(app, port, host) - Rack::Handler::WEBrick.run(app, Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0)) - end - - def set_server - Capybara.server = @server - end - - def set_driver - Capybara.default_driver = @browser.to_sym - end - - def set_port - Capybara.server_port = @port - end - end - end -end diff --git a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb new file mode 100644 index 0000000000..77533bf39b --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb @@ -0,0 +1,126 @@ +require 'rack/handler/puma' +require 'selenium-webdriver' + +module SystemTesting + module DriverAdapters + # == RailsSeleniumDriver for System Testing + # + # The RailsSeleniumDriver uses the Selenium 2.0 webdriver. The + # selenium-webdriver gem is required by this driver. + # + # The RailsSeleniumDriver is useful for real browser testing and + # supports Chrome and Firefox. + # + # By default Rails system testing will use Rails' configuration with Capybara + # and the Selenium driver. To explictly set the RailsSeleniumDriver + # add the following to your Rails' configuration test environment: + # + # config.system_testing.driver = :rails_selenium_driver + # + # Because this driver supports real browser testing it is required that a + # server is configured. + # + # If no server is specified when the driver is initialized, Puma will be used + # by default. The default settings for the RailsSeleniumDriver + # are as follows: + # + # # + # + # The settings for the RailsSeleniumDriver can be changed in the + # Rails configuration file. + # + # config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + # server: :webrick, + # port: 28100, + # screen_size: [ 800, 800 ] + # ) + # + # The default browser is set to Chrome because the current version of + # Firefox does not work with selenium-webdriver. If you want to use Firefox, + # you will need to use Firefox 45.0esr or 47.0 and ensure + # that selenium-webdriver is version 2.53.4. To change the browser from + # +:chrome+ to +:firefox+, initialize the Selenium driver in your Rails' + # test environment: + # + # config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + # browser: :firefox + # ) + class RailsSeleniumDriver + attr_reader :browser, :server, :port, :screen_size + + def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [ 1400,1400 ]) # :nodoc: + @browser = browser + @server = server + @port = port + @screen_size = screen_size + end + + def call # :nodoc: + registration + setup + end + + private + def registration + register_browser_driver + register_server + end + + def setup + set_server + set_driver + set_port + end + + def register_browser_driver + Capybara.register_driver @browser do |app| + Capybara::Selenium::Driver.new(app, browser: @browser).tap do |driver| + driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) + end + end + end + + def register_server + Capybara.register_server @server do |app, port, host| + case @server + when :puma + register_puma(app, port) + when :webrick + register_webrick(app, port, host) + else + register_default(app, port) + end + end + end + + def register_default(app, port) + Capybara.run_default_server(app, port) + end + + def register_puma(app, port) + ::Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') + end + + def register_webrick(app, port, host) + ::Rack::Handler::WEBrick.run(app, Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0)) + end + + def set_server + Capybara.server = @server + end + + def set_driver + Capybara.default_driver = @browser.to_sym + end + + def set_port + Capybara.server_port = @port + end + end + end +end diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb new file mode 100644 index 0000000000..b409fdfab0 --- /dev/null +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -0,0 +1,10 @@ +require 'abstract_unit' + +class CapybaraDriverTest < ActiveSupport::TestCase + def test_setting_useragent + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( + :rack_test + ) + assert_equal :rack_test, Rails::SystemTestCase.driver.name + end +end diff --git a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb deleted file mode 100644 index 0f037760d3..0000000000 --- a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'abstract_unit' - -class CapybaraRackTestDriverTest < ActiveSupport::TestCase - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::CapybaraRackTestDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal 'Capybara', Rails::SystemTestCase.driver.useragent - end - - def test_setting_useragent - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - useragent: 'x' - ) - assert_equal 'x', Rails::SystemTestCase.driver.useragent - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - made_up_arg: 'x' - ) - end - end -end diff --git a/actionpack/test/system_testing/capybara_selenium_driver_test.rb b/actionpack/test/system_testing/capybara_selenium_driver_test.rb deleted file mode 100644 index 6357b35aa6..0000000000 --- a/actionpack/test/system_testing/capybara_selenium_driver_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'abstract_unit' - -class CapybaraSeleniumDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :capybara_selenium_driver - end - - def test_setting_driver_adapter_to_selenium - assert_kind_of SystemTesting::DriverAdapters::CapybaraSeleniumDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :chrome, Rails::SystemTestCase.driver.browser - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size - end - - def test_setting_browser - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - browser: :firefox - ) - - assert_equal :firefox, Rails::SystemTestCase.driver.browser - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end - - def test_setting_screen_size - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - screen_size: [ 800, 800 ] - ) - - assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - made_up_arg: 'x' - ) - end - end -end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb index bfbeaa63a2..220a755bd3 100644 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -7,13 +7,7 @@ class DriverAdapterTest < ActiveSupport::TestCase end assert_nothing_raised do - Rails::SystemTestCase.driver = :capybara_selenium_driver - end - end - - test 'settings can only be used for the appropriate adapter' do - assert_raises(ArgumentError) do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new(something: 'test') + Rails::SystemTestCase.driver = :rack_test end end end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb new file mode 100644 index 0000000000..c239444838 --- /dev/null +++ b/actionpack/test/system_testing/rails_selenium_driver_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +class RailsSeleniumDriverTest < ActiveSupport::TestCase + def setup + Rails::SystemTestCase.driver = :rails_selenium_driver + end + + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::RailsSeleniumDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :chrome, Rails::SystemTestCase.driver.browser + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size + end + + def test_setting_browser + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + browser: :firefox + ) + + assert_equal :firefox, Rails::SystemTestCase.driver.browser + end + + def test_setting_server + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + server: :webrick + ) + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port + end + + def test_setting_screen_size + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + screen_size: [ 800, 800 ] + ) + + assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + made_up_arg: 'x' + ) + end + end +end -- cgit v1.2.3 From e9127f7aa082986952ffcc8331b675a3a99c3a83 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 2 Oct 2016 15:22:27 -0400 Subject: Add support for screenshots This change adds support, tests, and documentation for the screenshot helper. If taking screenshots is supported by the driver (for example Rack Test doesn't support screenshots) then a screenshot will be taken if the test fails. --- .../driver_adapters/capybara_driver.rb | 4 ++ .../driver_adapters/rails_selenium_driver.rb | 4 ++ actionpack/lib/system_testing/test_helper.rb | 4 +- actionpack/lib/system_testing/test_helpers.rb | 3 +- .../test_helpers/screenshot_helper.rb | 48 ++++++++++++++++++++++ .../test/system_testing/screenshot_helper_test.rb | 20 +++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 actionpack/lib/system_testing/test_helpers/screenshot_helper.rb create mode 100644 actionpack/test/system_testing/screenshot_helper_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb index 84941a9929..e40088970a 100644 --- a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb @@ -52,6 +52,10 @@ module SystemTesting def call Capybara.default_driver = @name end + + def supports_screenshots? + @name != :rack_test + end end end end diff --git a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb index 77533bf39b..723de3de48 100644 --- a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb @@ -65,6 +65,10 @@ module SystemTesting setup end + def supports_screenshots? + true + end + private def registration register_browser_driver diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index 2027a55d90..0ca4f792cb 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -3,8 +3,9 @@ require 'system_testing/test_helpers' module SystemTesting module TestHelper # :nodoc: - include TestHelpers::FormHelper include TestHelpers::Assertions + include TestHelpers::FormHelper + include TestHelpers::ScreenshotHelper include Capybara::DSL Capybara.app = Rack::Builder.new do @@ -14,6 +15,7 @@ module SystemTesting end def after_teardown + take_screenshot if supported? Capybara.reset_sessions! super end diff --git a/actionpack/lib/system_testing/test_helpers.rb b/actionpack/lib/system_testing/test_helpers.rb index 074a45d5c8..3c528a6953 100644 --- a/actionpack/lib/system_testing/test_helpers.rb +++ b/actionpack/lib/system_testing/test_helpers.rb @@ -2,7 +2,8 @@ module SystemTesting module TestHelpers extend ActiveSupport::Autoload - autoload :FormHelper autoload :Assertions + autoload :FormHelper + autoload :ScreenshotHelper end end diff --git a/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb new file mode 100644 index 0000000000..a74ebb8cae --- /dev/null +++ b/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb @@ -0,0 +1,48 @@ +module SystemTesting + module TestHelpers + # Screenshot helper for system testing + module ScreenshotHelper + # Takes a screenshot of the current page in the browser if the system + # test driver supports screenshots and the test failed. + # + # Additionally +take_screenshot+ can be used within your tests at points + # you want to take a screenshot if the driver supports screenshots. The + # Rack Test driver does not support screenshots. + # + # You can check of the driver supports screenshots by running + # + # Rails::SystemTestCase.driver.supports_screenshots? + # => true + def take_screenshot + puts "[Screenshot]: #{image_path}" + puts find_image + end + + private + def supported? + Rails::SystemTestCase.driver.supports_screenshots? && !passed? + end + + def image_path + path = "tmp/screenshots/failures_#{method_name}.png" + page.save_screenshot(Rails.root.join(path)) + path + end + + def find_image + if ENV['CAPYBARA_INLINE_SCREENSHOT'] == 'artifact' + "\e]1338;url=artifact://#{image_path}\a" + else + name = inline_base64(File.basename(image_path)) + image = inline_base64(File.read(image_path)) + "\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a" + end + end + + def inline_base64(path) + Base64.encode64(path).gsub("\n",'') + end + end + end +end + diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb new file mode 100644 index 0000000000..8bf2d0c8d0 --- /dev/null +++ b/actionpack/test/system_testing/screenshot_helper_test.rb @@ -0,0 +1,20 @@ +require 'abstract_unit' + +class ScreenshotHelperTest < ActiveSupport::TestCase + def test_driver_support_for_screenshots + Rails::SystemTestCase.driver = :rails_selenium_driver + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :rack_test + assert_not Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :selenium + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :webkit + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :poltergeist + assert Rails::SystemTestCase.driver.supports_screenshots? + end +end -- cgit v1.2.3 From c83e6d36dd13baa8b8cb48ce1c628788a2456d21 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 2 Oct 2016 17:37:57 -0400 Subject: Refactor so all drivers use Puma by default Puma is the default webserver of Rails. Because of this it doesn't make sense to run tests in Webkit if the default server is Puma. Here I've refactored the webserver to be it's own standalone module so it can be shared between Rails' selenium default driver and Capybara's defaut drivers. --- actionpack/lib/system_testing/driver_adapters.rb | 2 +- .../driver_adapters/capybara_driver.rb | 28 ++++++++++++-- .../driver_adapters/rails_selenium_driver.rb | 40 ++----------------- .../system_testing/driver_adapters/web_server.rb | 45 ++++++++++++++++++++++ .../test/system_testing/capybara_driver_test.rb | 35 +++++++++++++++-- 5 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 actionpack/lib/system_testing/driver_adapters/web_server.rb (limited to 'actionpack') diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb index bb65fe9111..d246106717 100644 --- a/actionpack/lib/system_testing/driver_adapters.rb +++ b/actionpack/lib/system_testing/driver_adapters.rb @@ -33,7 +33,7 @@ module SystemTesting # # => SystemTesting::DriverAdapters::RailsSeleniumDriver def lookup(driver) if CapybaraDriver::CAPYBARA_DEFAULTS.include?(driver) - CapybaraDriver.new(driver) + CapybaraDriver.new(name: driver) elsif driver.is_a?(Symbol) klass = const_get(driver.to_s.camelize) klass.new diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb index e40088970a..487c2b3b88 100644 --- a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb @@ -1,3 +1,5 @@ +require 'system_testing/driver_adapters/web_server' + module SystemTesting module DriverAdapters # == CapybaraDriver for System Testing @@ -41,21 +43,41 @@ module SystemTesting # server: :webrick # ) class CapybaraDriver + include WebServer + CAPYBARA_DEFAULTS = [ :rack_test, :selenium, :webkit, :poltergeist ] - attr_reader :name + attr_reader :name, :server, :port - def initialize(name) + def initialize(name: :rack_test, server: :puma, port: 28100) @name = name + @server = server + @port = port end def call - Capybara.default_driver = @name + registration + setup end def supports_screenshots? @name != :rack_test end + + private + def registration + register_server + end + + def setup + set_server + set_port + set_driver + end + + def set_driver + Capybara.default_driver = @name + end end end end diff --git a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb index 723de3de48..0e8fc00871 100644 --- a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb @@ -1,5 +1,4 @@ -require 'rack/handler/puma' -require 'selenium-webdriver' +require 'system_testing/driver_adapters/web_server' module SystemTesting module DriverAdapters @@ -51,6 +50,8 @@ module SystemTesting # browser: :firefox # ) class RailsSeleniumDriver + include WebServer + attr_reader :browser, :server, :port, :screen_size def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [ 1400,1400 ]) # :nodoc: @@ -77,8 +78,8 @@ module SystemTesting def setup set_server - set_driver set_port + set_driver end def register_browser_driver @@ -89,42 +90,9 @@ module SystemTesting end end - def register_server - Capybara.register_server @server do |app, port, host| - case @server - when :puma - register_puma(app, port) - when :webrick - register_webrick(app, port, host) - else - register_default(app, port) - end - end - end - - def register_default(app, port) - Capybara.run_default_server(app, port) - end - - def register_puma(app, port) - ::Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') - end - - def register_webrick(app, port, host) - ::Rack::Handler::WEBrick.run(app, Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0)) - end - - def set_server - Capybara.server = @server - end - def set_driver Capybara.default_driver = @browser.to_sym end - - def set_port - Capybara.server_port = @port - end end end end diff --git a/actionpack/lib/system_testing/driver_adapters/web_server.rb b/actionpack/lib/system_testing/driver_adapters/web_server.rb new file mode 100644 index 0000000000..85bcaed8b2 --- /dev/null +++ b/actionpack/lib/system_testing/driver_adapters/web_server.rb @@ -0,0 +1,45 @@ +begin + require "rack/handler/puma" +rescue LoadError + false +end + +module SystemTesting + module DriverAdapters + module WebServer # :nodoc: + def register_server + Capybara.register_server @server do |app, port, host| + case @server + when :puma + register_puma(app, port) + when :webrick + register_webrick(app, port, host) + else + register_default(app, port) + end + end + end + + private + def register_default(app, port) + Capybara.run_default_server(app, port) + end + + def register_puma(app, port) + Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') + end + + def register_webrick(app, port) + Rack::Handler::WEBrick.run(app, Port: port) + end + + def set_server + Capybara.server = @server + end + + def set_port + Capybara.server_port = @port + end + end + end +end diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb index b409fdfab0..a6be63ba7f 100644 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -1,10 +1,39 @@ require 'abstract_unit' class CapybaraDriverTest < ActiveSupport::TestCase - def test_setting_useragent + def setup + Rails::SystemTestCase.driver = :poltergeist + end + + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::CapybaraDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :poltergeist, Rails::SystemTestCase.driver.name + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + end + + def test_setting_driver + Rails::SystemTestCase.driver = :webkit + + assert_equal :webkit, Rails::SystemTestCase.driver.name + end + + def test_setting_server Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - :rack_test + server: :webrick ) - assert_equal :rack_test, Rails::SystemTestCase.driver.name + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port end end -- cgit v1.2.3 From a21e18d5080a2c4808330271885f5664a725d3f3 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Tue, 4 Oct 2016 08:48:21 -0400 Subject: Appease Rubocop Rubocop / code climate don't like single quotes and prefer doubles. --- actionpack/lib/system_test_case.rb | 4 ++-- actionpack/lib/system_testing/driver_adapter.rb | 2 +- actionpack/lib/system_testing/driver_adapters/capybara_driver.rb | 2 +- .../lib/system_testing/driver_adapters/rails_selenium_driver.rb | 2 +- actionpack/lib/system_testing/driver_adapters/web_server.rb | 2 +- actionpack/lib/system_testing/railtie.rb | 2 +- actionpack/lib/system_testing/test_helper.rb | 4 ++-- actionpack/lib/system_testing/test_helpers/form_helper.rb | 2 +- actionpack/lib/system_testing/test_helpers/screenshot_helper.rb | 5 ++--- actionpack/test/system_testing/capybara_driver_test.rb | 2 +- actionpack/test/system_testing/driver_adapter_test.rb | 4 ++-- actionpack/test/system_testing/rails_selenium_driver_test.rb | 4 ++-- actionpack/test/system_testing/screenshot_helper_test.rb | 2 +- 13 files changed, 18 insertions(+), 19 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb index 196798495f..373cb4f0d0 100644 --- a/actionpack/lib/system_test_case.rb +++ b/actionpack/lib/system_test_case.rb @@ -1,5 +1,5 @@ -require 'system_testing/test_helper' -require 'system_testing/driver_adapter' +require "system_testing/test_helper" +require "system_testing/driver_adapter" module Rails # System tests are similar to Integration tests in that they incorporate multiple diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb index 3d8545f3c6..ccd702e967 100644 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ b/actionpack/lib/system_testing/driver_adapter.rb @@ -1,4 +1,4 @@ -require 'system_testing/driver_adapters' +require "system_testing/driver_adapters" module SystemTesting # The SystemTesting::DriverAdapter module is used to load the driver diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb index 487c2b3b88..7755b9616a 100644 --- a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb @@ -1,4 +1,4 @@ -require 'system_testing/driver_adapters/web_server' +require "system_testing/driver_adapters/web_server" module SystemTesting module DriverAdapters diff --git a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb index 0e8fc00871..aa67ba8029 100644 --- a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb +++ b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb @@ -1,4 +1,4 @@ -require 'system_testing/driver_adapters/web_server' +require "system_testing/driver_adapters/web_server" module SystemTesting module DriverAdapters diff --git a/actionpack/lib/system_testing/driver_adapters/web_server.rb b/actionpack/lib/system_testing/driver_adapters/web_server.rb index 85bcaed8b2..65d719d1ab 100644 --- a/actionpack/lib/system_testing/driver_adapters/web_server.rb +++ b/actionpack/lib/system_testing/driver_adapters/web_server.rb @@ -26,7 +26,7 @@ module SystemTesting end def register_puma(app, port) - Rack::Handler::Puma.run(app, Port: port, Threads: '0:4') + Rack::Handler::Puma.run(app, Port: port, Threads: "0:4") end def register_webrick(app, port) diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb index 459a8e9623..d8af69e5d3 100644 --- a/actionpack/lib/system_testing/railtie.rb +++ b/actionpack/lib/system_testing/railtie.rb @@ -1,4 +1,4 @@ -require 'system_test_case' +require "system_test_case" module SystemTesting # = System Testing Railtie diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb index 0ca4f792cb..8bd49b2f4c 100644 --- a/actionpack/lib/system_testing/test_helper.rb +++ b/actionpack/lib/system_testing/test_helper.rb @@ -1,5 +1,5 @@ -require 'capybara/dsl' -require 'system_testing/test_helpers' +require "capybara/dsl" +require "system_testing/test_helpers" module SystemTesting module TestHelper # :nodoc: diff --git a/actionpack/lib/system_testing/test_helpers/form_helper.rb b/actionpack/lib/system_testing/test_helpers/form_helper.rb index 4789694bbb..74a1516116 100644 --- a/actionpack/lib/system_testing/test_helpers/form_helper.rb +++ b/actionpack/lib/system_testing/test_helpers/form_helper.rb @@ -50,7 +50,7 @@ module SystemTesting find(:field, name, visible: :all, checked: checked) end - def find_label_wrapper(field, location: './ancestor::label') + def find_label_wrapper(field, location: "./ancestor::label") field.find :xpath, location end end diff --git a/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb index a74ebb8cae..eeade229df 100644 --- a/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb +++ b/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb @@ -30,7 +30,7 @@ module SystemTesting end def find_image - if ENV['CAPYBARA_INLINE_SCREENSHOT'] == 'artifact' + if ENV["CAPYBARA_INLINE_SCREENSHOT"] == "artifact" "\e]1338;url=artifact://#{image_path}\a" else name = inline_base64(File.basename(image_path)) @@ -40,9 +40,8 @@ module SystemTesting end def inline_base64(path) - Base64.encode64(path).gsub("\n",'') + Base64.encode64(path).gsub("\n", "") end end end end - diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb index a6be63ba7f..129fe95f25 100644 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class CapybaraDriverTest < ActiveSupport::TestCase def setup diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb index 220a755bd3..035d018adf 100644 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -1,7 +1,7 @@ -require 'abstract_unit' +require "abstract_unit" class DriverAdapterTest < ActiveSupport::TestCase - test 'only registered adapters are accepted' do + test "only registered adapters are accepted" do assert_raises(NameError) do Rails::SystemTestCase.driver = :whatever end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb index c239444838..25fc6ca097 100644 --- a/actionpack/test/system_testing/rails_selenium_driver_test.rb +++ b/actionpack/test/system_testing/rails_selenium_driver_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class RailsSeleniumDriverTest < ActiveSupport::TestCase def setup @@ -51,7 +51,7 @@ class RailsSeleniumDriverTest < ActiveSupport::TestCase def test_does_not_accept_nonsense_kwargs assert_raises ArgumentError do Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - made_up_arg: 'x' + made_up_arg: "x" ) end end diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb index 8bf2d0c8d0..8060c499df 100644 --- a/actionpack/test/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/system_testing/screenshot_helper_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class ScreenshotHelperTest < ActiveSupport::TestCase def test_driver_support_for_screenshots -- cgit v1.2.3 From 5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 6 Nov 2016 18:55:15 -0500 Subject: Turn system testing into it's own gem and rename Renames `Rails::SystemTestCase` to `ActionSystemTest` and moves it to a gem under the Rails name. We need to name the class `ActionSystemTestCase` because the gem expects a module but tests themselves expect a class. Adds MIT-LICENSE, CHANGELOG, and README for the future. --- actionpack/actionpack.gemspec | 1 - actionpack/lib/system_test_case.rb | 57 ------------- actionpack/lib/system_testing/driver_adapter.rb | 31 ------- actionpack/lib/system_testing/driver_adapters.rb | 46 ---------- .../driver_adapters/capybara_driver.rb | 83 ------------------ .../driver_adapters/rails_selenium_driver.rb | 98 ---------------------- .../system_testing/driver_adapters/web_server.rb | 45 ---------- actionpack/lib/system_testing/railtie.rb | 17 ---- actionpack/lib/system_testing/test_helper.rb | 23 ----- actionpack/lib/system_testing/test_helpers.rb | 9 -- .../lib/system_testing/test_helpers/assertions.rb | 40 --------- .../lib/system_testing/test_helpers/form_helper.rb | 58 ------------- .../test_helpers/screenshot_helper.rb | 47 ----------- actionpack/test/abstract_unit.rb | 1 - .../test/system_testing/capybara_driver_test.rb | 39 --------- .../test/system_testing/driver_adapter_test.rb | 13 --- .../system_testing/rails_selenium_driver_test.rb | 58 ------------- .../test/system_testing/screenshot_helper_test.rb | 20 ----- 18 files changed, 686 deletions(-) delete mode 100644 actionpack/lib/system_test_case.rb delete mode 100644 actionpack/lib/system_testing/driver_adapter.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters/capybara_driver.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb delete mode 100644 actionpack/lib/system_testing/driver_adapters/web_server.rb delete mode 100644 actionpack/lib/system_testing/railtie.rb delete mode 100644 actionpack/lib/system_testing/test_helper.rb delete mode 100644 actionpack/lib/system_testing/test_helpers.rb delete mode 100644 actionpack/lib/system_testing/test_helpers/assertions.rb delete mode 100644 actionpack/lib/system_testing/test_helpers/form_helper.rb delete mode 100644 actionpack/lib/system_testing/test_helpers/screenshot_helper.rb delete mode 100644 actionpack/test/system_testing/capybara_driver_test.rb delete mode 100644 actionpack/test/system_testing/driver_adapter_test.rb delete mode 100644 actionpack/test/system_testing/rails_selenium_driver_test.rb delete mode 100644 actionpack/test/system_testing/screenshot_helper_test.rb (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index bb9079dc1c..2c24a54305 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -26,7 +26,6 @@ Gem::Specification.new do |s| s.add_dependency "rails-html-sanitizer", "~> 1.0", ">= 1.0.2" s.add_dependency "rails-dom-testing", "~> 2.0" s.add_dependency "actionview", version - s.add_dependency "capybara", "~> 2.7.0" s.add_development_dependency "activemodel", version end diff --git a/actionpack/lib/system_test_case.rb b/actionpack/lib/system_test_case.rb deleted file mode 100644 index 373cb4f0d0..0000000000 --- a/actionpack/lib/system_test_case.rb +++ /dev/null @@ -1,57 +0,0 @@ -require "system_testing/test_helper" -require "system_testing/driver_adapter" - -module Rails - # 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 - # Rails::SystemTestCase. 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. - # - # A system test looks like the following: - # - # require 'test_helper' - # - # class Users::CreateTest < Rails::SystemTestCase - # def adding_a_new_user - # visit users_path - # click_on 'New User' - # - # fill_in 'Name', with: 'Arya' - # click_on 'Create User' - # - # assert_text 'Arya' - # end - # end - # - # System test driver can be configured in your Rails configuration file for the - # test environment. - # - # config.system_testing.driver = :rails_selenium_driver - # - # 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 = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - # browser: :firefox - # ) - # - # A list of supported adapters can be found in DriverAdapters. - # - # If you want to use one of the default drivers provided by Capybara you can - # set the driver in your config to one of those defaults: +:rack_test+, - # +:selenium+, +:webkit+, or +:poltergeist+. These 4 drivers use Capyara's - # driver defaults whereas the RailsSeleniumDriver has pre-set - # configuration for browser, server, port, etc. - class SystemTestCase < ActionDispatch::IntegrationTest - include SystemTesting::TestHelper - include SystemTesting::DriverAdapter - - ActiveSupport.run_load_hooks(:system_testing, self) - end -end diff --git a/actionpack/lib/system_testing/driver_adapter.rb b/actionpack/lib/system_testing/driver_adapter.rb deleted file mode 100644 index ccd702e967..0000000000 --- a/actionpack/lib/system_testing/driver_adapter.rb +++ /dev/null @@ -1,31 +0,0 @@ -require "system_testing/driver_adapters" - -module SystemTesting - # The SystemTesting::DriverAdapter module is used to load the driver - # set in your Rails' test configuration file. - # - # The default driver adapter is the +:rails_selenium_driver+. - 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) - end - - # Specify the adapter and settings for the system test driver set in the - # Rails' configuration file. - def driver=(driver) - @driver = DriverAdapters.lookup(driver) - @driver.call - end - end - end -end diff --git a/actionpack/lib/system_testing/driver_adapters.rb b/actionpack/lib/system_testing/driver_adapters.rb deleted file mode 100644 index d246106717..0000000000 --- a/actionpack/lib/system_testing/driver_adapters.rb +++ /dev/null @@ -1,46 +0,0 @@ -module SystemTesting - # == System Testing Driver Adapters - # - # By default Rails supports Capybara with the Selenium Driver. Rails provides - # configuration setup for using the selenium driver with Capybara. - # Additionally Rails can be used as a layer between Capybara and its other - # supported drivers: +:rack_test+, +:selenium+, +:webkit+, or +:poltergeist+. - # - # *{RackTest}[https://github.com/jnicklas/capybara#racktest] - # *{Selenium}[http://seleniumhq.org/docs/01_introducing_selenium.html#selenium-2-aka-selenium-webdriver] - # *{Webkit}[https://github.com/thoughtbot/capybara-webkit] - # *{Poltergeist}[https://github.com/teampoltergeist/poltergeist] - # - # === Driver Features - # - # | | Default Browser | Supports Screenshots? | - # |-----------------|-----------------------|-----------------------| - # | Rails' Selenium | Chrome | Yes | - # | Rack Test | No JS Support | No | - # | Selenium | Firefox | Yes | - # | WebKit | Headless w/ Qt | Yes | - # | Poltergeist | Headless w/ PhantomJS | Yes | - module DriverAdapters - extend ActiveSupport::Autoload - - autoload :CapybaraDriver - autoload :RailsSeleniumDriver - - class << self - # Returns driver for specified name. - # - # SystemTesting::DriverAdapters.lookup(:rails_selenium_driver) - # # => SystemTesting::DriverAdapters::RailsSeleniumDriver - def lookup(driver) - if CapybaraDriver::CAPYBARA_DEFAULTS.include?(driver) - CapybaraDriver.new(name: driver) - elsif driver.is_a?(Symbol) - klass = const_get(driver.to_s.camelize) - klass.new - else - driver - end - end - end - end -end diff --git a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb b/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb deleted file mode 100644 index 7755b9616a..0000000000 --- a/actionpack/lib/system_testing/driver_adapters/capybara_driver.rb +++ /dev/null @@ -1,83 +0,0 @@ -require "system_testing/driver_adapters/web_server" - -module SystemTesting - module DriverAdapters - # == CapybaraDriver for System Testing - # - # The CapybaraDriver is a shim that sits between Rails and - # Capybara. - # - # The drivers Capybara supports are: +:rack_test+, +:selenium+, +:webkit+, - # and +:poltergeist+. - # - # Rails provides its own defaults for Capybara with the Selenium driver - # through RailsSeleniumDriver, but allows users to use Selenium - # directly. - # - # To set your system tests to use one of Capybara's default drivers, add - # the following to yur Rails' configuration test environment: - # - # config.system_testing.driver = :rack_test - # - # The +:rack_test+ driver is a basic test driver that doesn't support - # JavaScript testing and doesn't require a server. - # - # The +:poltergeist+ and +:webkit+ drivers are headless, but require some - # extra environment setup. Because the default server for Rails is Puma, each - # of the Capybara drivers will default to using Puma. Changing the configuration - # to use Webrick is possible by initalizing a new driver object. - # - # The default settings for the CapybaraDriver are: - # - # # - # - # The settings for the CapybaraDriver can be changed from - # Rails' configuration file. - # - # config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - # name: :webkit, - # server: :webrick - # ) - class CapybaraDriver - include WebServer - - CAPYBARA_DEFAULTS = [ :rack_test, :selenium, :webkit, :poltergeist ] - - attr_reader :name, :server, :port - - def initialize(name: :rack_test, server: :puma, port: 28100) - @name = name - @server = server - @port = port - end - - def call - registration - setup - end - - def supports_screenshots? - @name != :rack_test - end - - private - def registration - register_server - end - - def setup - set_server - set_port - set_driver - end - - def set_driver - Capybara.default_driver = @name - end - end - end -end diff --git a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb b/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb deleted file mode 100644 index aa67ba8029..0000000000 --- a/actionpack/lib/system_testing/driver_adapters/rails_selenium_driver.rb +++ /dev/null @@ -1,98 +0,0 @@ -require "system_testing/driver_adapters/web_server" - -module SystemTesting - module DriverAdapters - # == RailsSeleniumDriver for System Testing - # - # The RailsSeleniumDriver uses the Selenium 2.0 webdriver. The - # selenium-webdriver gem is required by this driver. - # - # The RailsSeleniumDriver is useful for real browser testing and - # supports Chrome and Firefox. - # - # By default Rails system testing will use Rails' configuration with Capybara - # and the Selenium driver. To explictly set the RailsSeleniumDriver - # add the following to your Rails' configuration test environment: - # - # config.system_testing.driver = :rails_selenium_driver - # - # Because this driver supports real browser testing it is required that a - # server is configured. - # - # If no server is specified when the driver is initialized, Puma will be used - # by default. The default settings for the RailsSeleniumDriver - # are as follows: - # - # # - # - # The settings for the RailsSeleniumDriver can be changed in the - # Rails configuration file. - # - # config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - # server: :webrick, - # port: 28100, - # screen_size: [ 800, 800 ] - # ) - # - # The default browser is set to Chrome because the current version of - # Firefox does not work with selenium-webdriver. If you want to use Firefox, - # you will need to use Firefox 45.0esr or 47.0 and ensure - # that selenium-webdriver is version 2.53.4. To change the browser from - # +:chrome+ to +:firefox+, initialize the Selenium driver in your Rails' - # test environment: - # - # config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - # browser: :firefox - # ) - class RailsSeleniumDriver - include WebServer - - attr_reader :browser, :server, :port, :screen_size - - def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [ 1400,1400 ]) # :nodoc: - @browser = browser - @server = server - @port = port - @screen_size = screen_size - end - - def call # :nodoc: - registration - setup - end - - def supports_screenshots? - true - end - - private - def registration - register_browser_driver - register_server - end - - def setup - set_server - set_port - set_driver - end - - def register_browser_driver - Capybara.register_driver @browser do |app| - Capybara::Selenium::Driver.new(app, browser: @browser).tap do |driver| - driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) - end - end - end - - def set_driver - Capybara.default_driver = @browser.to_sym - end - end - end -end diff --git a/actionpack/lib/system_testing/driver_adapters/web_server.rb b/actionpack/lib/system_testing/driver_adapters/web_server.rb deleted file mode 100644 index 65d719d1ab..0000000000 --- a/actionpack/lib/system_testing/driver_adapters/web_server.rb +++ /dev/null @@ -1,45 +0,0 @@ -begin - require "rack/handler/puma" -rescue LoadError - false -end - -module SystemTesting - module DriverAdapters - module WebServer # :nodoc: - def register_server - Capybara.register_server @server do |app, port, host| - case @server - when :puma - register_puma(app, port) - when :webrick - register_webrick(app, port, host) - else - register_default(app, port) - end - end - end - - private - def register_default(app, port) - Capybara.run_default_server(app, port) - end - - def register_puma(app, port) - Rack::Handler::Puma.run(app, Port: port, Threads: "0:4") - end - - def register_webrick(app, port) - Rack::Handler::WEBrick.run(app, Port: port) - end - - def set_server - Capybara.server = @server - end - - def set_port - Capybara.server_port = @port - end - end - end -end diff --git a/actionpack/lib/system_testing/railtie.rb b/actionpack/lib/system_testing/railtie.rb deleted file mode 100644 index d8af69e5d3..0000000000 --- a/actionpack/lib/system_testing/railtie.rb +++ /dev/null @@ -1,17 +0,0 @@ -require "system_test_case" - -module SystemTesting - # = System Testing Railtie - class Railtie < Rails::Railtie # :nodoc: - config.system_testing = ActiveSupport::OrderedOptions.new - - initializer "system_testing.set_configs" do |app| - options = app.config.system_testing - options.driver ||= Rails::SystemTestCase.default_driver - - ActiveSupport.on_load(:system_testing) do - options.each { |k,v| send("#{k}=", v) } - end - end - end -end diff --git a/actionpack/lib/system_testing/test_helper.rb b/actionpack/lib/system_testing/test_helper.rb deleted file mode 100644 index 8bd49b2f4c..0000000000 --- a/actionpack/lib/system_testing/test_helper.rb +++ /dev/null @@ -1,23 +0,0 @@ -require "capybara/dsl" -require "system_testing/test_helpers" - -module SystemTesting - module TestHelper # :nodoc: - include TestHelpers::Assertions - include TestHelpers::FormHelper - include TestHelpers::ScreenshotHelper - include Capybara::DSL - - Capybara.app = Rack::Builder.new do - map "/" do - run Rails.application - end - end - - def after_teardown - take_screenshot if supported? - Capybara.reset_sessions! - super - end - end -end diff --git a/actionpack/lib/system_testing/test_helpers.rb b/actionpack/lib/system_testing/test_helpers.rb deleted file mode 100644 index 3c528a6953..0000000000 --- a/actionpack/lib/system_testing/test_helpers.rb +++ /dev/null @@ -1,9 +0,0 @@ -module SystemTesting - module TestHelpers - extend ActiveSupport::Autoload - - autoload :Assertions - autoload :FormHelper - autoload :ScreenshotHelper - end -end diff --git a/actionpack/lib/system_testing/test_helpers/assertions.rb b/actionpack/lib/system_testing/test_helpers/assertions.rb deleted file mode 100644 index 6e5b62e3c5..0000000000 --- a/actionpack/lib/system_testing/test_helpers/assertions.rb +++ /dev/null @@ -1,40 +0,0 @@ -module SystemTesting - module TestHelpers - # Assertions for system testing that aren't included by default in Capybara. - # These are assertions that are useful specifically for Rails applications. - module Assertions - # Asserts that all of the provided selectors are present on the given page. - # - # assert_all_of_selectors('p', 'td') - def assert_all_of_selectors(*items) - options = items.extract_options! - type = type_for_selector(items) - - items.each do |item| - assert_selector type, item, options - end - end - - # Asserts that none of the provided selectors are present on the page. - # - # assert_none_of_selectors('ul', 'ol') - def assert_none_of_selectors(*items) - options = items.extract_options! - type = type_for_selector(items) - - items.each do |item| - assert_no_selector type, item, options - end - end - - private - def type_for_selector(*items) - if items.first.is_a?(Symbol) - items.shift - else - Capybara.default_selector - end - end - end - end -end diff --git a/actionpack/lib/system_testing/test_helpers/form_helper.rb b/actionpack/lib/system_testing/test_helpers/form_helper.rb deleted file mode 100644 index 74a1516116..0000000000 --- a/actionpack/lib/system_testing/test_helpers/form_helper.rb +++ /dev/null @@ -1,58 +0,0 @@ -module SystemTesting - module TestHelpers - # Form helpers for system testing that aren't included by default in - # Capybara. - module FormHelper - # Finds all provided fields or text areas and fills in with supplied values. - # - # fill_in_all_fields('Name' => 'Eileen', 'Job Title' => 'Programmer') - def fill_in_all_fields(fields) - fields.each do |name, value| - fill_in name, with: value - end - end - - # Locates a checkbox that is present inside a label and checks it. When - # using styled boxes Selenium may not be able to see the checkbox. This - # form helper looks inside the checkbox and clicks the label instead of - # setting the value of the checkbox. - # - # click_checkbox_label 'Admin' - # - # By default +click_checkbox_label+ looks for checkboxes that are not - # checked by default. To locate an already checked box and uncheck it - # set checked to true: - # - # click_checkbox_label 'Admin', checked: true - def click_checkbox_label(name, checked: false) - field = find_checkbox(name, checked) - label = find_label_wrapper(field) - label.click - end - - # In lieu of locating a button and calling +click_on+, +press_enter+ will - # submit the form via enter. This method will only work for drivers that - # load a browser like Selenium. - # - # test 'Adding a User' do - # fill_in 'Name', with: 'Arya' - # - # press_enter - # - # assert_text 'Arya' - # end - def press_enter - page.driver.browser.action.send_keys(:enter).perform - end - - private - def find_checkbox(name, checked) - find(:field, name, visible: :all, checked: checked) - end - - def find_label_wrapper(field, location: "./ancestor::label") - field.find :xpath, location - end - end - end -end diff --git a/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb deleted file mode 100644 index eeade229df..0000000000 --- a/actionpack/lib/system_testing/test_helpers/screenshot_helper.rb +++ /dev/null @@ -1,47 +0,0 @@ -module SystemTesting - module TestHelpers - # Screenshot helper for system testing - module ScreenshotHelper - # Takes a screenshot of the current page in the browser if the system - # test driver supports screenshots and the test failed. - # - # Additionally +take_screenshot+ can be used within your tests at points - # you want to take a screenshot if the driver supports screenshots. The - # Rack Test driver does not support screenshots. - # - # You can check of the driver supports screenshots by running - # - # Rails::SystemTestCase.driver.supports_screenshots? - # => true - def take_screenshot - puts "[Screenshot]: #{image_path}" - puts find_image - end - - private - def supported? - Rails::SystemTestCase.driver.supports_screenshots? && !passed? - end - - def image_path - path = "tmp/screenshots/failures_#{method_name}.png" - page.save_screenshot(Rails.root.join(path)) - path - end - - def find_image - if ENV["CAPYBARA_INLINE_SCREENSHOT"] == "artifact" - "\e]1338;url=artifact://#{image_path}\a" - else - name = inline_base64(File.basename(image_path)) - image = inline_base64(File.read(image_path)) - "\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a" - end - end - - def inline_base64(path) - Base64.encode64(path).gsub("\n", "") - end - end - end -end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 3b35af6d3a..459b0d6c54 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -33,7 +33,6 @@ require "action_view/testing/resolvers" require "action_dispatch" require "active_support/dependencies" require "active_model" -require "system_test_case" require "pp" # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb deleted file mode 100644 index 129fe95f25..0000000000 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ /dev/null @@ -1,39 +0,0 @@ -require "abstract_unit" - -class CapybaraDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :poltergeist - end - - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::CapybaraDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :poltergeist, Rails::SystemTestCase.driver.name - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - end - - def test_setting_driver - Rails::SystemTestCase.driver = :webkit - - assert_equal :webkit, Rails::SystemTestCase.driver.name - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end -end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb deleted file mode 100644 index 035d018adf..0000000000 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "abstract_unit" - -class DriverAdapterTest < ActiveSupport::TestCase - test "only registered adapters are accepted" do - assert_raises(NameError) do - Rails::SystemTestCase.driver = :whatever - end - - assert_nothing_raised do - Rails::SystemTestCase.driver = :rack_test - end - end -end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb deleted file mode 100644 index 25fc6ca097..0000000000 --- a/actionpack/test/system_testing/rails_selenium_driver_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require "abstract_unit" - -class RailsSeleniumDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :rails_selenium_driver - end - - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::RailsSeleniumDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :chrome, Rails::SystemTestCase.driver.browser - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size - end - - def test_setting_browser - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - browser: :firefox - ) - - assert_equal :firefox, Rails::SystemTestCase.driver.browser - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end - - def test_setting_screen_size - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - screen_size: [ 800, 800 ] - ) - - assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - made_up_arg: "x" - ) - end - end -end diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb deleted file mode 100644 index 8060c499df..0000000000 --- a/actionpack/test/system_testing/screenshot_helper_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -require "abstract_unit" - -class ScreenshotHelperTest < ActiveSupport::TestCase - def test_driver_support_for_screenshots - Rails::SystemTestCase.driver = :rails_selenium_driver - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :rack_test - assert_not Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :selenium - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :webkit - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :poltergeist - assert Rails::SystemTestCase.driver.supports_screenshots? - end -end -- cgit v1.2.3 From 1a0ca84a064b07ecab798793a3d7ebe89bb6367c Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 11:50:42 -0500 Subject: Move and rename system tests * Move system tests back into Action Pack * Rename `ActionSystemTest` to `ActionDispatch::SystemTestCase` * Remove private base module and only make file for public `SystemTestCase` class, name private module `SystemTesting` * Rename `ActionSystemTestCase` to `ApplicationSystemTestCase` * Update corresponding documentation and guides * Delete old `ActionSystemTest` files --- actionpack/lib/action_dispatch.rb | 2 + actionpack/lib/action_dispatch/system_test_case.rb | 129 +++++++++++++++++++++ .../lib/action_dispatch/system_testing/browser.rb | 28 +++++ .../lib/action_dispatch/system_testing/driver.rb | 18 +++ .../lib/action_dispatch/system_testing/server.rb | 23 ++++ .../test_helpers/screenshot_helper.rb | 53 +++++++++ .../test/dispatch/system_testing/browser_test.rb | 10 ++ .../test/dispatch/system_testing/driver_test.rb | 9 ++ .../system_testing/screenshot_helper_test.rb | 10 ++ .../test/dispatch/system_testing/server_test.rb | 10 ++ .../system_testing/system_test_case_test.rb | 21 ++++ 11 files changed, 313 insertions(+) create mode 100644 actionpack/lib/action_dispatch/system_test_case.rb create mode 100644 actionpack/lib/action_dispatch/system_testing/browser.rb create mode 100644 actionpack/lib/action_dispatch/system_testing/driver.rb create mode 100644 actionpack/lib/action_dispatch/system_testing/server.rb create mode 100644 actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb create mode 100644 actionpack/test/dispatch/system_testing/browser_test.rb create mode 100644 actionpack/test/dispatch/system_testing/driver_test.rb create mode 100644 actionpack/test/dispatch/system_testing/screenshot_helper_test.rb create mode 100644 actionpack/test/dispatch/system_testing/server_test.rb create mode 100644 actionpack/test/dispatch/system_testing/system_test_case_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index 028177ace2..303790e96d 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -97,6 +97,8 @@ module ActionDispatch autoload :TestResponse autoload :AssertionResponse end + + autoload :SystemTestCase, "action_dispatch/system_test_case" end autoload :Mime, "action_dispatch/http/mime_type" diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb new file mode 100644 index 0000000000..850f101ca8 --- /dev/null +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -0,0 +1,129 @@ +require "capybara/dsl" +require "action_controller" +require "action_dispatch/system_testing/driver" +require "action_dispatch/system_testing/server" +require "action_dispatch/system_testing/browser" +require "action_dispatch/system_testing/test_helpers/screenshot_helper" + +module ActionDispatch + class SystemTestCase < IntegrationTest + # = System Testing + # + # System tests let you test real application in the browser. Because system + # tests use a real browser experience you can test all of your JavaScript + # easily from your test suite. + # + # To create a system test in your application, extend your test class + # from ApplicationSystemTestCase. System tests use Capybara as a + # base and allow you to configure the settings through your + # system_test_helper.rb file that is generated with a new + # application or scaffold. + # + # Here is an example system test: + # + # require 'system_test_helper' + # + # class Users::CreateTest < ApplicationSystemTestCase + # test "adding a new user" do + # visit users_path + # click_on 'New User' + # + # fill_in 'Name', with: 'Arya' + # click_on 'Create User' + # + # assert_text 'Arya' + # end + # end + # + # 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. + # + # require "test_helper" + # + # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + # teardown do + # take_failed_screenshot + # Capybara.reset_sessions! + # end + # end + # + # By default, ActionDispatch::SystemTestCase is driven by the + # Selenium driver, with the Chrome browser, and a browser size of 1400x1400. + # + # Changing the driver configuration options are easy. Let's say you want to use + # and the Firefox browser instead. In your +system_test_helper.rb+ + # file add the following: + # + # require "test_helper" + # + # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + # driven_by :selenium, using: :firefox + # + # teardown do + # take_failed_screenshot + # Capybara.reset_sessions! + # end + # end + # + # +driven_by+ has a required argument for the driver name. The keyword + # arguments are +:using+ for the browser (not applicable for headless drivers), + # and +:screen_size+ to change the size of the screen taking screenshots. + # + # To use a headless driver, like Poltergeist, update your Gemfile to use + # Poltergeist instead of Selenium and then declare the driver name in the + # +system_test_helper.rb+ file. In this case you would leave out the +:using+ + # option because the driver is headless. + # + # require "test_helper" + # require "capybara/poltergeist" + # + # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + # driven_by :poltergeist + # + # teardown do + # take_failed_screenshot + # Capybara.reset_sessions! + # end + # end + # + # Because ActionDispatch::SystemTestCase is a shim between Capybara + # and Rails, any driver that is supported by Capybara is supported by system + # tests as long as you include the required gems and files. + include Capybara::DSL + include SystemTesting::TestHelpers::ScreenshotHelper + + def self.start_application # :nodoc: + Capybara.app = Rack::Builder.new do + map "/" do + run Rails.application + end + end + end + + # System Test configuration options + # + # The defaults settings are Selenium, using Chrome, with a screen size + # of 1400x1400. + # + # Examples: + # + # driven_by :poltergeist + # + # driven_by :selenium, using: :firefox + # + # driven_by :selenium, screen_size: [800, 800] + def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400]) + SystemTesting::Driver.new(driver).run + SystemTesting::Server.new.run + SystemTesting::Browser.new(using, screen_size).run if selenium?(driver) + end + + def self.selenium?(driver) # :nodoc: + driver == :selenium + end + end + + SystemTestCase.start_application +end diff --git a/actionpack/lib/action_dispatch/system_testing/browser.rb b/actionpack/lib/action_dispatch/system_testing/browser.rb new file mode 100644 index 0000000000..c9a6628516 --- /dev/null +++ b/actionpack/lib/action_dispatch/system_testing/browser.rb @@ -0,0 +1,28 @@ +module ActionDispatch + module SystemTesting + class Browser # :nodoc: + def initialize(name, screen_size) + @name = name + @screen_size = screen_size + end + + def run + register + setup + end + + private + def register + Capybara.register_driver @name do |app| + Capybara::Selenium::Driver.new(app, browser: @name).tap do |driver| + driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) + end + end + end + + def setup + Capybara.default_driver = @name.to_sym + end + end + end +end diff --git a/actionpack/lib/action_dispatch/system_testing/driver.rb b/actionpack/lib/action_dispatch/system_testing/driver.rb new file mode 100644 index 0000000000..7c2ad84e19 --- /dev/null +++ b/actionpack/lib/action_dispatch/system_testing/driver.rb @@ -0,0 +1,18 @@ +module ActionDispatch + module SystemTesting + class Driver # :nodoc: + def initialize(name) + @name = name + end + + def run + register + end + + private + def register + Capybara.default_driver = @name + end + end + end +end diff --git a/actionpack/lib/action_dispatch/system_testing/server.rb b/actionpack/lib/action_dispatch/system_testing/server.rb new file mode 100644 index 0000000000..62ba07736b --- /dev/null +++ b/actionpack/lib/action_dispatch/system_testing/server.rb @@ -0,0 +1,23 @@ +require "rack/handler/puma" + +module ActionDispatch + module SystemTesting + class Server # :nodoc: + def run + register + setup + end + + private + def register + Capybara.register_server :rails_puma do |app, port, host| + Rack::Handler::Puma.run(app, Port: port, Threads: "0:1") + end + end + + def setup + Capybara.server = :rails_puma + end + end + end +end diff --git a/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb new file mode 100644 index 0000000000..1ad64b7f78 --- /dev/null +++ b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb @@ -0,0 +1,53 @@ +module ActionDispatch + module SystemTesting + module TestHelpers + # Screenshot helper for system testing + module ScreenshotHelper + # Takes a screenshot of the current page in the browser. + # + # +take_screenshot+ can be used at any point in your system tests to take + # a screenshot of the current state. This can be useful for debugging or + # automating visual testing. + def take_screenshot + save_image + puts "[Screenshot]: #{image_path}" + puts display_image + end + + # Takes a screenshot of the current page in the browser if the test + # failed. + # + # +take_screenshot+ is included in system_test_helper.rb that is + # generated with the application. To take screenshots when a test fails + # add +take_failed_screenshot+ to the teardown block before clearing + # sessions. + def take_failed_screenshot + take_screenshot unless passed? + end + + private + def image_path + "tmp/screenshots/failures_#{method_name}.png" + end + + def save_image + page.save_screenshot(Rails.root.join(image_path)) + end + + def display_image + if ENV["CAPYBARA_INLINE_SCREENSHOT"] == "artifact" + "\e]1338;url=artifact://#{image_path}\a" + else + name = inline_base64(File.basename(image_path)) + image = inline_base64(File.read(image_path)) + "\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a" + end + end + + def inline_base64(path) + Base64.encode64(path).gsub("\n", "") + end + end + end + end +end diff --git a/actionpack/test/dispatch/system_testing/browser_test.rb b/actionpack/test/dispatch/system_testing/browser_test.rb new file mode 100644 index 0000000000..b0ad309492 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/browser_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "action_dispatch/system_testing/browser" + +class BrowserTest < ActiveSupport::TestCase + test "initializing the browser" do + browser = ActionDispatch::SystemTesting::Browser.new(:chrome, [ 1400, 1400 ]) + assert_equal :chrome, browser.instance_variable_get(:@name) + assert_equal [ 1400, 1400 ], browser.instance_variable_get(:@screen_size) + end +end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb new file mode 100644 index 0000000000..f0ebdb38db --- /dev/null +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -0,0 +1,9 @@ +require "abstract_unit" +require "action_dispatch/system_testing/driver" + +class DriverTest < ActiveSupport::TestCase + test "initializing the driver" do + driver = ActionDispatch::SystemTesting::Driver.new(:selenium) + assert_equal :selenium, driver.instance_variable_get(:@name) + end +end diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb new file mode 100644 index 0000000000..3da89f4a10 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "action_dispatch/system_testing/test_helpers/screenshot_helper" + +class ScreenshotHelperTest < ActiveSupport::TestCase + test "image path is saved in tmp directory" do + new_test = ActionDispatch::SystemTestCase.new("x") + + assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + end +end diff --git a/actionpack/test/dispatch/system_testing/server_test.rb b/actionpack/test/dispatch/system_testing/server_test.rb new file mode 100644 index 0000000000..66842f4ea9 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/server_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "capybara/dsl" +require "action_dispatch/system_testing/server" + +class ServerTest < ActiveSupport::TestCase + test "initializing the server port" do + server = ActionDispatch::SystemTesting::Server.new.run + assert_includes Capybara.servers, :rails_puma + end +end diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb new file mode 100644 index 0000000000..a384902a14 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -0,0 +1,21 @@ +require "abstract_unit" + +class SystemTestCaseTest < ActiveSupport::TestCase + test "driven_by sets Capybara's default driver to poltergeist" do + ActionDispatch::SystemTestCase.driven_by :poltergeist + + assert_equal :poltergeist, Capybara.default_driver + end + + test "driven_by sets Capybara's drivers respectively" do + ActionDispatch::SystemTestCase.driven_by :selenium, using: :chrome + + assert_includes Capybara.drivers, :selenium + assert_includes Capybara.drivers, :chrome + assert_equal :chrome, Capybara.default_driver + end + + test "selenium? returns false if driver is poltergeist" do + assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist) + end +end -- cgit v1.2.3 From 983275eb7c01eaeba5798a422514f4d9d6b74786 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 12:12:06 -0500 Subject: Fix screenshot helper to provide correct file name We only want the file name to include the word `failures` if it failed, not any time the user wants to take a screenshot during a test run. --- .../system_testing/test_helpers/screenshot_helper.rb | 8 ++++++-- .../test/dispatch/system_testing/screenshot_helper_test.rb | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb index 1ad64b7f78..ab14910b41 100644 --- a/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +++ b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb @@ -17,7 +17,7 @@ module ActionDispatch # Takes a screenshot of the current page in the browser if the test # failed. # - # +take_screenshot+ is included in system_test_helper.rb that is + # +take_failed_screenshot+ is included in system_test_helper.rb that is # generated with the application. To take screenshots when a test fails # add +take_failed_screenshot+ to the teardown block before clearing # sessions. @@ -26,8 +26,12 @@ module ActionDispatch end private + def image_name + passed? ? method_name : "failures_#{method_name}" + end + def image_path - "tmp/screenshots/failures_#{method_name}.png" + "tmp/screenshots/#{image_name}.png" end def save_image diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb index 3da89f4a10..8c14f799b0 100644 --- a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -5,6 +5,14 @@ class ScreenshotHelperTest < ActiveSupport::TestCase test "image path is saved in tmp directory" do new_test = ActionDispatch::SystemTestCase.new("x") - assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + assert_equal "tmp/screenshots/x.png", new_test.send(:image_path) + end + + test "image path includes failures text if test did not pass" do + new_test = ActionDispatch::SystemTestCase.new("x") + + new_test.stub :passed?, false do + assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + end end end -- cgit v1.2.3 From 161cf89e134267f9b579f493ca19b12c30d5fd36 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 17:49:21 -0500 Subject: Fix default host in setup, move teardown to helper file * Override integration test default host Integration tests automatically set the default host to 'http://example.com'. This works fine for integration tests because they are not real browser sessions, but doesn't work fine for system tests because they are real browser sessions. We can override this by setting the `host!` in `before_setup. The `Capybara.always_include_port` will allow the test to look at `127.0.0.1:port capybara picks` and properly redirect the test. Any application can override this by setting the `host!` in their system test helper. Generally though, applications are going to be using localhost. In this commit I also moved the setup and teardown into their own module for tidiness. * Move teardown settings into system test case These configuration options can be put into the system test case file instead of the generated system tests helper file. This is an implementation detail and therefore shouldn't be generated with the template. --- actionpack/lib/action_dispatch/system_test_case.rb | 2 ++ .../lib/action_dispatch/system_testing/server.rb | 9 +++++++++ .../test_helpers/setup_and_teardown.rb | 20 ++++++++++++++++++++ .../test/dispatch/system_testing/server_test.rb | 9 ++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb index 850f101ca8..91cbc1180c 100644 --- a/actionpack/lib/action_dispatch/system_test_case.rb +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -4,6 +4,7 @@ require "action_dispatch/system_testing/driver" require "action_dispatch/system_testing/server" require "action_dispatch/system_testing/browser" require "action_dispatch/system_testing/test_helpers/screenshot_helper" +require "action_dispatch/system_testing/test_helpers/setup_and_teardown" module ActionDispatch class SystemTestCase < IntegrationTest @@ -92,6 +93,7 @@ module ActionDispatch # and Rails, any driver that is supported by Capybara is supported by system # tests as long as you include the required gems and files. include Capybara::DSL + include SystemTesting::TestHelpers::SetupAndTeardown include SystemTesting::TestHelpers::ScreenshotHelper def self.start_application # :nodoc: diff --git a/actionpack/lib/action_dispatch/system_testing/server.rb b/actionpack/lib/action_dispatch/system_testing/server.rb index 62ba07736b..4a214ef713 100644 --- a/actionpack/lib/action_dispatch/system_testing/server.rb +++ b/actionpack/lib/action_dispatch/system_testing/server.rb @@ -16,8 +16,17 @@ module ActionDispatch end def setup + set_server + set_port + end + + def set_server Capybara.server = :rails_puma end + + def set_port + Capybara.always_include_port = true + end end end end diff --git a/actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb b/actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb new file mode 100644 index 0000000000..491559eedf --- /dev/null +++ b/actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb @@ -0,0 +1,20 @@ +module ActionDispatch + module SystemTesting + module TestHelpers + module SetupAndTeardown # :nodoc: + DEFAULT_HOST = "127.0.0.1" + + def before_setup + host! DEFAULT_HOST + super + end + + def after_teardown + super + take_failed_screenshot + Capybara.reset_sessions! + end + end + end + end +end diff --git a/actionpack/test/dispatch/system_testing/server_test.rb b/actionpack/test/dispatch/system_testing/server_test.rb index 66842f4ea9..10412d6367 100644 --- a/actionpack/test/dispatch/system_testing/server_test.rb +++ b/actionpack/test/dispatch/system_testing/server_test.rb @@ -3,8 +3,15 @@ require "capybara/dsl" require "action_dispatch/system_testing/server" class ServerTest < ActiveSupport::TestCase + setup do + ActionDispatch::SystemTesting::Server.new.run + end + test "initializing the server port" do - server = ActionDispatch::SystemTesting::Server.new.run assert_includes Capybara.servers, :rails_puma end + + test "port is always included" do + assert Capybara.always_include_port, "expected Capybara.always_include_port to be true" + end end -- cgit v1.2.3 From 2d61c5d846f8dd3a02080fedce7ab63b8d314db6 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 20 Feb 2017 14:38:46 -0500 Subject: Rename system_test_helper -> application_system_test_case This renames the system test helper file to be application system test case to match what the rest of Rails does. In the future we should consider changing the test_helper to match. --- actionpack/lib/action_dispatch/system_test_case.rb | 25 ++++++---------------- .../test_helpers/screenshot_helper.rb | 6 +++--- 2 files changed, 9 insertions(+), 22 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb index 91cbc1180c..ca23cab6ae 100644 --- a/actionpack/lib/action_dispatch/system_test_case.rb +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -17,12 +17,12 @@ module ActionDispatch # To create a system test in your application, extend your test class # from ApplicationSystemTestCase. System tests use Capybara as a # base and allow you to configure the settings through your - # system_test_helper.rb file that is generated with a new + # application_system_test_case.rb file that is generated with a new # application or scaffold. # # Here is an example system test: # - # require 'system_test_helper' + # require 'application_system_test_case' # # class Users::CreateTest < ApplicationSystemTestCase # test "adding a new user" do @@ -36,7 +36,7 @@ module ActionDispatch # end # end # - # When generating an application or scaffold a +system_test_helper.rb+ will also + # When generating an application or scaffold a +application_system_test_case.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. @@ -44,28 +44,20 @@ module ActionDispatch # require "test_helper" # # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - # teardown do - # take_failed_screenshot - # Capybara.reset_sessions! - # end + # driven_by :selenium, using: :chrome, screen_size: [1400, 1400] # end # # By default, ActionDispatch::SystemTestCase is driven by the # Selenium driver, with the Chrome browser, and a browser size of 1400x1400. # # Changing the driver configuration options are easy. Let's say you want to use - # and the Firefox browser instead. In your +system_test_helper.rb+ + # and the Firefox browser instead. In your +application_system_test_case.rb+ # file add the following: # # require "test_helper" # # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase # driven_by :selenium, using: :firefox - # - # teardown do - # take_failed_screenshot - # Capybara.reset_sessions! - # end # end # # +driven_by+ has a required argument for the driver name. The keyword @@ -74,7 +66,7 @@ module ActionDispatch # # To use a headless driver, like Poltergeist, update your Gemfile to use # Poltergeist instead of Selenium and then declare the driver name in the - # +system_test_helper.rb+ file. In this case you would leave out the +:using+ + # +application_system_test_case.rb+ file. In this case you would leave out the +:using+ # option because the driver is headless. # # require "test_helper" @@ -82,11 +74,6 @@ module ActionDispatch # # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase # driven_by :poltergeist - # - # teardown do - # take_failed_screenshot - # Capybara.reset_sessions! - # end # end # # Because ActionDispatch::SystemTestCase is a shim between Capybara diff --git a/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb index ab14910b41..784005cb93 100644 --- a/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +++ b/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb @@ -17,9 +17,9 @@ module ActionDispatch # Takes a screenshot of the current page in the browser if the test # failed. # - # +take_failed_screenshot+ is included in system_test_helper.rb that is - # generated with the application. To take screenshots when a test fails - # add +take_failed_screenshot+ to the teardown block before clearing + # +take_failed_screenshot+ is included in application_system_test_case.rb + # that is generated with the application. To take screenshots when a test + # fails add +take_failed_screenshot+ to the teardown block before clearing # sessions. def take_failed_screenshot take_screenshot unless passed? -- cgit v1.2.3 From 42a6dbdf8480b96c4a0ac6e1dab215ae9dd77d1d Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 20 Feb 2017 16:08:20 -0500 Subject: Clean up documentation There were some grammar issues and incorrect information in the system tests documentation. --- actionpack/lib/action_dispatch/system_test_case.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb index ca23cab6ae..276e3161bd 100644 --- a/actionpack/lib/action_dispatch/system_test_case.rb +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -10,7 +10,7 @@ module ActionDispatch class SystemTestCase < IntegrationTest # = System Testing # - # System tests let you test real application in the browser. Because system + # System tests let you test applications in the browser. Because system # tests use a real browser experience you can test all of your JavaScript # easily from your test suite. # @@ -36,10 +36,10 @@ module ActionDispatch # end # end # - # When generating an application or scaffold a +application_system_test_case.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. + # When generating an application or scaffold a +application_system_test_case.rb+ + # file 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. # # require "test_helper" # @@ -51,7 +51,7 @@ module ActionDispatch # Selenium driver, with the Chrome browser, and a browser size of 1400x1400. # # Changing the driver configuration options are easy. Let's say you want to use - # and the Firefox browser instead. In your +application_system_test_case.rb+ + # the Firefox browser instead of Chrome. In your +application_system_test_case.rb+ # file add the following: # # require "test_helper" @@ -61,8 +61,9 @@ module ActionDispatch # end # # +driven_by+ has a required argument for the driver name. The keyword - # arguments are +:using+ for the browser (not applicable for headless drivers), - # and +:screen_size+ to change the size of the screen taking screenshots. + # arguments are +:using+ for the browser and +:screen_size+ to change the + # size of the browser screen. These two options are not applicable for + # headless drivers and will be silently ignored if passed. # # To use a headless driver, like Poltergeist, update your Gemfile to use # Poltergeist instead of Selenium and then declare the driver name in the @@ -93,7 +94,7 @@ module ActionDispatch # System Test configuration options # - # The defaults settings are Selenium, using Chrome, with a screen size + # The default settings are Selenium, using Chrome, with a screen size # of 1400x1400. # # Examples: -- cgit v1.2.3