diff options
5 files changed, 107 insertions, 43 deletions
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 |