diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2019-01-29 17:54:16 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-29 17:54:16 -0500 |
commit | 94d54fa4ab641a0ddeb173409cb41cc5becc02a9 (patch) | |
tree | 0d9d2aa005388fb7980226068c720e6d362c451c | |
parent | 3d22069c6355dc60be65e01958cf32917bc53142 (diff) | |
parent | 1a4f61307286702205fd32b3aa62c26c21c9cce3 (diff) | |
download | rails-94d54fa4ab641a0ddeb173409cb41cc5becc02a9.tar.gz rails-94d54fa4ab641a0ddeb173409cb41cc5becc02a9.tar.bz2 rails-94d54fa4ab641a0ddeb173409cb41cc5becc02a9.zip |
Merge pull request #35081 from eileencodes/ec-driver-option-updated
[UPDATED] Implement a way to add browser capabilities
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 16 | ||||
-rw-r--r-- | actionpack/CHANGELOG.md | 8 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/system_test_case.rb | 24 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/system_testing/browser.rb | 21 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/system_testing/driver.rb | 9 | ||||
-rw-r--r-- | actionpack/test/dispatch/system_testing/driver_test.rb | 67 | ||||
-rw-r--r-- | railties/test/application/test_runner_test.rb | 3 |
8 files changed, 132 insertions, 17 deletions
@@ -10,6 +10,7 @@ gemspec gem "rake", ">= 11.1" gem "capybara", ">= 2.15" +gem "selenium-webdriver", "~> 3.5.1" gem "rack-cache", "~> 1.2" gem "sass-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 1f02b9c4c7..dfbd796969 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -238,10 +238,10 @@ GEM faye-websocket (0.10.7) eventmachine (>= 0.12.0) websocket-driver (>= 0.5.1) - ffi (1.9.25) - ffi (1.9.25-java) - ffi (1.9.25-x64-mingw32) - ffi (1.9.25-x86-mingw32) + ffi (1.10.0) + ffi (1.10.0-java) + ffi (1.10.0-x64-mingw32) + ffi (1.10.0-x86-mingw32) fugit (1.1.6) et-orbi (~> 1.1, >= 1.1.6) raabro (~> 1.1) @@ -336,6 +336,7 @@ GEM mysql2 (0.5.2-x64-mingw32) mysql2 (0.5.2-x86-mingw32) nio4r (2.3.1) + nio4r (2.3.1-java) nokogiri (1.9.1) mini_portile2 (~> 2.4.0) nokogiri (1.9.1-java) @@ -432,9 +433,9 @@ GEM tilt (>= 1.1, < 3) sdoc (1.0.0) rdoc (>= 5.0) - selenium-webdriver (3.141.0) + selenium-webdriver (3.5.2) childprocess (~> 0.5) - rubyzip (~> 1.2, >= 1.2.2) + rubyzip (~> 1.0) sequel (5.14.0) serverengine (2.0.7) sigdump (~> 0.2.2) @@ -509,6 +510,8 @@ GEM websocket (1.2.8) websocket-driver (0.7.0) websocket-extensions (>= 0.1.0) + websocket-driver (0.7.0-java) + websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) xpath (3.2.0) nokogiri (~> 1.8) @@ -568,6 +571,7 @@ DEPENDENCIES rubocop (>= 0.47) sass-rails sdoc (~> 1.0) + selenium-webdriver (~> 3.5.1) sequel sidekiq sneakers diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 1d2f6b09c3..d9041aecb7 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,9 @@ +* `ActionDispatch::SystemTestCase.driven_by` can now be called with a block + to define specific browser capabilities. + + *Edouard Chin* + + ## Rails 6.0.0.beta1 (January 18, 2019) ## * Remove deprecated `fragment_cache_key` helper in favor of `combined_fragment_cache_key`. @@ -154,7 +160,7 @@ *Aaron Kromer* -* Pass along arguments to underlying `get` method in `follow_redirect!`. +* Pass along arguments to underlying `get` method in `follow_redirect!` Now all arguments passed to `follow_redirect!` are passed to the underlying `get` method. This for example allows to set custom headers for the diff --git a/actionpack/lib/action_dispatch/system_test_case.rb b/actionpack/lib/action_dispatch/system_test_case.rb index c74c0ccced..484eb46331 100644 --- a/actionpack/lib/action_dispatch/system_test_case.rb +++ b/actionpack/lib/action_dispatch/system_test_case.rb @@ -89,6 +89,24 @@ module ActionDispatch # { js_errors: true } # end # + # Some drivers require browser capabilities to be passed as a block instead + # of through the +options+ hash. + # + # As an example, if you want to add mobile emulation on chrome, you'll have to + # create an instance of selenium's `Chrome::Options` object and add + # capabilities with a block. + # + # The block will be passed an instance of `<Driver>::Options` where you can + # define the capabilities you want. Please refer to your driver documentation + # to learn about supported options. + # + # class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + # driven_by :chrome, screen_size: [1024, 768] do |driver_option| + # driver_option.add_emulation(device: 'iPhone 6') + # driver_option.add_extension('path/to/chrome_extension.crx') + # end + # end + # # Because <tt>ActionDispatch::SystemTestCase</tt> 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. @@ -134,8 +152,10 @@ module ActionDispatch # driven_by :selenium, using: :firefox # # driven_by :selenium, using: :headless_firefox - def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}) - self.driver = SystemTesting::Driver.new(driver, using: using, screen_size: screen_size, options: options) + def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities) + driver_options = { using: using, screen_size: screen_size, options: options } + + self.driver = SystemTesting::Driver.new(driver, driver_options, &capabilities) end driven_by :selenium diff --git a/actionpack/lib/action_dispatch/system_testing/browser.rb b/actionpack/lib/action_dispatch/system_testing/browser.rb index 1b0bce6b9e..f691bd5fe5 100644 --- a/actionpack/lib/action_dispatch/system_testing/browser.rb +++ b/actionpack/lib/action_dispatch/system_testing/browser.rb @@ -29,20 +29,27 @@ module ActionDispatch end end + def capabilities + @option ||= case type + when :chrome + Selenium::WebDriver::Chrome::Options.new + when :firefox + Selenium::WebDriver::Firefox::Options.new + end + end + private def headless_chrome_browser_options - options = Selenium::WebDriver::Chrome::Options.new - options.args << "--headless" - options.args << "--disable-gpu" if Gem.win_platform? + capability.args << "--headless" + capability.args << "--disable-gpu" if Gem.win_platform? - options + capabilities end def headless_firefox_browser_options - options = Selenium::WebDriver::Firefox::Options.new - options.args << "-headless" + capabilities.args << "-headless" - options + capabilities end end end diff --git a/actionpack/lib/action_dispatch/system_testing/driver.rb b/actionpack/lib/action_dispatch/system_testing/driver.rb index 5252ff6746..25a09dd918 100644 --- a/actionpack/lib/action_dispatch/system_testing/driver.rb +++ b/actionpack/lib/action_dispatch/system_testing/driver.rb @@ -3,11 +3,12 @@ module ActionDispatch module SystemTesting class Driver # :nodoc: - def initialize(name, **options) + def initialize(name, **options, &capabilities) @name = name @browser = Browser.new(options[:using]) @screen_size = options[:screen_size] @options = options[:options] + @capabilities = capabilities end def use @@ -22,6 +23,8 @@ module ActionDispatch end def register + define_browser_capabilities(@browser.capabilities) + Capybara.register_driver @name do |app| case @name when :selenium then register_selenium(app) @@ -31,6 +34,10 @@ module ActionDispatch end end + def define_browser_capabilities(capabilities) + @capabilities.call(capabilities) if @capabilities + end + def browser_options @options.merge(options: @browser.options).compact end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index a824ee0c84..2ab8fac666 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -2,6 +2,7 @@ require "abstract_unit" require "action_dispatch/system_testing/driver" +require "selenium/webdriver" class DriverTest < ActiveSupport::TestCase test "initializing the driver" do @@ -51,4 +52,70 @@ class DriverTest < ActiveSupport::TestCase test "registerable? returns false if driver is rack_test" do assert_not ActionDispatch::SystemTesting::Driver.new(:rack_test).send(:registerable?) end + + test "define extra capabilities using chrome" do + driver_option = nil + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :chrome) do |option| + option.add_argument("start-maximized") + option.add_emulation(device_name: "iphone 6") + option.add_preference(:detach, true) + + driver_option = option + end + driver.use + + expected = { args: ["start-maximized"], mobileEmulation: { deviceName: "iphone 6" }, prefs: { detach: true } } + assert_equal expected, driver_option.as_json + end + + test "define extra capabilities using headless_chrome" do + driver_option = nil + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :headless_chrome) do |option| + option.add_argument("start-maximized") + option.add_emulation(device_name: "iphone 6") + option.add_preference(:detach, true) + + driver_option = option + end + driver.use + + expected = { args: ["start-maximized"], mobileEmulation: { deviceName: "iphone 6" }, prefs: { detach: true } } + assert_equal expected, driver_option.as_json + end + + test "define extra capabilities using firefox" do + driver_option = nil + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :firefox) do |option| + option.add_preference("browser.startup.homepage", "http://www.seleniumhq.com/") + option.add_argument("--host=127.0.0.1") + + driver_option = option + end + driver.use + + expected = { "moz:firefoxOptions" => { args: ["--host=127.0.0.1"], prefs: { "browser.startup.homepage" => "http://www.seleniumhq.com/" } } } + assert_equal expected, driver_option.as_json + end + + test "define extra capabilities using headless_firefox" do + driver_option = nil + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :headless_firefox) do |option| + option.add_preference("browser.startup.homepage", "http://www.seleniumhq.com/") + option.add_argument("--host=127.0.0.1") + + driver_option = option + end + driver.use + + expected = { "moz:firefoxOptions" => { args: ["--host=127.0.0.1"], prefs: { "browser.startup.homepage" => "http://www.seleniumhq.com/" } } } + assert_equal expected, driver_option.as_json + end + + test "does not define extra capabilities" do + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :firefox) + + assert_nothing_raised do + driver.use + end + end end diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index fda6df500d..f706e53e5c 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -742,6 +742,7 @@ module ApplicationTests def test_reset_sessions_before_rollback_on_system_tests app_file "test/system/reset_session_before_rollback_test.rb", <<-RUBY require "application_system_test_case" + require "selenium/webdriver" class ResetSessionBeforeRollbackTest < ApplicationSystemTestCase def teardown_fixtures @@ -770,6 +771,7 @@ module ApplicationTests def test_reset_sessions_on_failed_system_test_screenshot app_file "test/system/reset_sessions_on_failed_system_test_screenshot_test.rb", <<~RUBY require "application_system_test_case" + require "selenium/webdriver" class ResetSessionsOnFailedSystemTestScreenshotTest < ApplicationSystemTestCase ActionDispatch::SystemTestCase.class_eval do @@ -826,6 +828,7 @@ module ApplicationTests def test_system_tests_are_run_through_rake_test_when_given_in_TEST app_file "test/system/dummy_test.rb", <<-RUBY require "application_system_test_case" + require "selenium/webdriver" class DummyTest < ApplicationSystemTestCase test "something" do |