From 5372459aa406a8ebb398191fe587f29d964b3c54 Mon Sep 17 00:00:00 2001 From: YusukeIwaki Date: Thu, 15 Jul 2021 11:40:03 +0900 Subject: [PATCH 1/2] Remove `poltergeist` and `webkit` driver registration logics in system testing. Add `cuprite` instead. Poltergeist and capybara-webkit are already not maintained. * https://github.com/teampoltergeist/poltergeist * https://github.com/thoughtbot/capybara-webkit Most users actually use Selenium with Headless Chrome in these years, and it is not encouraged to use poltergeist or capybara-webkit at this moment. Cuprite is a good alternative driver to Poltergeist: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing Not only removing deprecated Capybara drivers, also introducing Cuprite. --- actionpack/CHANGELOG.md | 8 ++++++++ .../lib/action_dispatch/system_test_case.rb | 10 +++++----- .../lib/action_dispatch/system_testing/driver.rb | 15 ++++----------- .../test/dispatch/system_testing/driver_test.rb | 15 ++++----------- guides/source/testing.md | 6 +++--- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index be38297390f..1c18bd2d01f 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,11 @@ +* Remove logic of `poltergeist` and `webkit` (capybara-webkit) driver registration for system testing. Add `cuprite` instead. + + [Poltergeist](https://github.com/teampoltergeist/poltergeist) and [capybara-webkit](https://github.com/thoughtbot/capybara-webkit) are already not maintained. These usage in Rails are removed for avoiding confusing users. + + [Cuprite](https://github.com/rubycdp/cuprite) is a good alternative to Poltergeist. Some guide descriptions are replaced from Poltergeist to Cuprite. + + *Yusuke Iwaki* + * Add `Middleware#remove` to delete middleware or raise if not found. `Middleware#remove` works just like `Middleware#delete` but will diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb index c757d2d5a1a..a1d0a507a85 100644 --- a/actionpack/lib/action_dispatch/system_test_case.rb +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -72,8 +72,8 @@ module ActionDispatch # Headless browsers such as headless Chrome and headless Firefox are also supported. # You can use these browsers by setting the +:using+ argument to +:headless_chrome+ or +:headless_firefox+. # - # To use a headless driver, like Poltergeist, update your Gemfile to use - # Poltergeist instead of Selenium and then declare the driver name in the + # To use a headless driver, like Cuprite, update your Gemfile to use + # Cuprite instead of Selenium and then declare the driver name in the # +application_system_test_case.rb+ file. In this case, you would leave out # the +:using+ option because the driver is headless, but you can still use # +:screen_size+ to change the size of the browser screen, also you can use @@ -81,10 +81,10 @@ module ActionDispatch # driver documentation to learn about supported options. # # require "test_helper" - # require "capybara/poltergeist" + # require "capybara/cuprite" # # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - # driven_by :poltergeist, screen_size: [1400, 1400], options: + # driven_by :cuprite, screen_size: [1400, 1400], options: # { js_errors: true } # end # @@ -140,7 +140,7 @@ module ActionDispatch # # Examples: # - # driven_by :poltergeist + # driven_by :cuprite # # driven_by :selenium, screen_size: [800, 800] # diff --git a/actionpack/lib/action_dispatch/system_testing/driver.rb b/actionpack/lib/action_dispatch/system_testing/driver.rb index 51cfa63e685..9fdc7f2a0e5 100644 --- a/actionpack/lib/action_dispatch/system_testing/driver.rb +++ b/actionpack/lib/action_dispatch/system_testing/driver.rb @@ -26,7 +26,7 @@ module ActionDispatch private def registerable? - [:selenium, :poltergeist, :webkit, :rack_test].include?(@name) + [:selenium, :cuprite, :rack_test].include?(@name) end def register @@ -35,8 +35,7 @@ module ActionDispatch Capybara.register_driver @name do |app| case @name when :selenium then register_selenium(app) - when :poltergeist then register_poltergeist(app) - when :webkit then register_webkit(app) + when :cuprite then register_cuprite(app) when :rack_test then register_rack_test(app) end end @@ -52,14 +51,8 @@ module ActionDispatch end end - def register_poltergeist(app) - Capybara::Poltergeist::Driver.new(app, @options.merge(window_size: @screen_size)) - end - - def register_webkit(app) - Capybara::Webkit::Driver.new(app, Capybara::Webkit::Configuration.to_hash.merge(@options)).tap do |driver| - driver.resize_window_to(driver.current_window_handle, *@screen_size) - end + def register_cuprite(app) + Capybara::Cuprite::Driver.new(app, @options.merge(window_size: @screen_size)) end def register_rack_test(app) diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index 6fe348483f1..8f10ba72b5f 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -37,20 +37,13 @@ class DriverTest < ActiveSupport::TestCase assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options) end - test "initializing the driver with a poltergeist" do - driver = ActionDispatch::SystemTesting::Driver.new(:poltergeist, screen_size: [1400, 1400], options: { js_errors: false }) - assert_equal :poltergeist, driver.instance_variable_get(:@name) + test "initializing the driver with a cuprite" do + driver = ActionDispatch::SystemTesting::Driver.new(:cuprite, screen_size: [1400, 1400], options: { js_errors: false }) + assert_equal :cuprite, driver.instance_variable_get(:@name) assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) assert_equal ({ js_errors: false }), driver.instance_variable_get(:@options) end - test "initializing the driver with a webkit" do - driver = ActionDispatch::SystemTesting::Driver.new(:webkit, screen_size: [1400, 1400], options: { skip_image_loading: true }) - assert_equal :webkit, driver.instance_variable_get(:@name) - assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) - assert_equal ({ skip_image_loading: true }), driver.instance_variable_get(:@options) - end - test "define extra capabilities using chrome" do driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :chrome) do |option| option.add_argument("start-maximized") @@ -153,6 +146,6 @@ class DriverTest < ActiveSupport::TestCase assert ActionDispatch::SystemTesting::Driver.new(:selenium).instance_variable_get(:@browser) assert_nil ActionDispatch::SystemTesting::Driver.new(:rack_test).instance_variable_get(:@browser) - assert_nil ActionDispatch::SystemTesting::Driver.new(:poltergeist).instance_variable_get(:@browser) + assert_nil ActionDispatch::SystemTesting::Driver.new(:cuprite).instance_variable_get(:@browser) end end diff --git a/guides/source/testing.md b/guides/source/testing.md index b10905b5f64..af759ce42a0 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -825,15 +825,15 @@ system tests should live. If you want to change the default settings you can change what the system tests are "driven by". Say you want to change the driver from Selenium to -Poltergeist. First add the `poltergeist` gem to your `Gemfile`. Then in your +Cuprite. First add the `cuprite` gem to your `Gemfile`. Then in your `application_system_test_case.rb` file do the following: ```ruby require "test_helper" -require "capybara/poltergeist" +require "capybara/cuprite" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - driven_by :poltergeist + driven_by :cuprite end ``` From e489b2467b727549dd04dab09b6be7ec21026cd0 Mon Sep 17 00:00:00 2001 From: YusukeIwaki Date: Tue, 20 Jul 2021 23:10:58 +0900 Subject: [PATCH 2/2] Deprecate :poltergeist, :webkit instead of removing. --- actionpack/CHANGELOG.md | 2 +- .../action_dispatch/system_testing/driver.rb | 22 ++++++++++++++++++- .../dispatch/system_testing/driver_test.rb | 18 +++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 1c18bd2d01f..6bd62814583 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,4 +1,4 @@ -* Remove logic of `poltergeist` and `webkit` (capybara-webkit) driver registration for system testing. Add `cuprite` instead. +* Deprecate `poltergeist` and `webkit` (capybara-webkit) driver registration for system testing (they will be removed in Rails 7.1). Add `cuprite` instead. [Poltergeist](https://github.com/teampoltergeist/poltergeist) and [capybara-webkit](https://github.com/thoughtbot/capybara-webkit) are already not maintained. These usage in Rails are removed for avoiding confusing users. diff --git a/actionpack/lib/action_dispatch/system_testing/driver.rb b/actionpack/lib/action_dispatch/system_testing/driver.rb index 9fdc7f2a0e5..f39ccd4cb55 100644 --- a/actionpack/lib/action_dispatch/system_testing/driver.rb +++ b/actionpack/lib/action_dispatch/system_testing/driver.rb @@ -9,6 +9,14 @@ module ActionDispatch @options = options[:options] || {} @capabilities = capabilities + if [:poltergeist, :webkit].include?(name) + ActiveSupport::Deprecation.warn <<~MSG.squish + Poltergeist and capybara-webkit are not maintained already. + Driver registration of :poltergeist or :webkit is deprecated and will be removed in Rails 7.1. + You can still use :selenium, and also :cuprite is available for alternative to Poltergeist. + MSG + end + if name == :selenium require "selenium/webdriver" @browser = Browser.new(options[:using]) @@ -26,7 +34,7 @@ module ActionDispatch private def registerable? - [:selenium, :cuprite, :rack_test].include?(@name) + [:selenium, :poltergeist, :webkit, :cuprite, :rack_test].include?(@name) end def register @@ -35,6 +43,8 @@ module ActionDispatch Capybara.register_driver @name do |app| case @name when :selenium then register_selenium(app) + when :poltergeist then register_poltergeist(app) + when :webkit then register_webkit(app) when :cuprite then register_cuprite(app) when :rack_test then register_rack_test(app) end @@ -51,6 +61,16 @@ module ActionDispatch end end + def register_poltergeist(app) + Capybara::Poltergeist::Driver.new(app, @options.merge(window_size: @screen_size)) + end + + def register_webkit(app) + Capybara::Webkit::Driver.new(app, Capybara::Webkit::Configuration.to_hash.merge(@options)).tap do |driver| + driver.resize_window_to(driver.current_window_handle, *@screen_size) + end + end + def register_cuprite(app) Capybara::Cuprite::Driver.new(app, @options.merge(window_size: @screen_size)) end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index 8f10ba72b5f..0e3cf65906c 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -37,6 +37,24 @@ class DriverTest < ActiveSupport::TestCase assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options) end + test "initializing the driver with a poltergeist" do + driver = assert_deprecated do + ActionDispatch::SystemTesting::Driver.new(:poltergeist, screen_size: [1400, 1400], options: { js_errors: false }) + end + assert_equal :poltergeist, driver.instance_variable_get(:@name) + assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) + assert_equal ({ js_errors: false }), driver.instance_variable_get(:@options) + end + + test "initializing the driver with a webkit" do + driver = assert_deprecated do + ActionDispatch::SystemTesting::Driver.new(:webkit, screen_size: [1400, 1400], options: { skip_image_loading: true }) + end + assert_equal :webkit, driver.instance_variable_get(:@name) + assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) + assert_equal ({ skip_image_loading: true }), driver.instance_variable_get(:@options) + end + test "initializing the driver with a cuprite" do driver = ActionDispatch::SystemTesting::Driver.new(:cuprite, screen_size: [1400, 1400], options: { js_errors: false }) assert_equal :cuprite, driver.instance_variable_get(:@name)