diff options
author | eileencodes <eileencodes@gmail.com> | 2016-10-02 17:37:57 -0400 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2017-02-20 15:07:33 -0500 |
commit | c83e6d36dd13baa8b8cb48ce1c628788a2456d21 (patch) | |
tree | b7ff88d26cdea08b357cebf77e158547f1b84edd /actionpack | |
parent | e9127f7aa082986952ffcc8331b675a3a99c3a83 (diff) | |
download | rails-c83e6d36dd13baa8b8cb48ce1c628788a2456d21.tar.gz rails-c83e6d36dd13baa8b8cb48ce1c628788a2456d21.tar.bz2 rails-c83e6d36dd13baa8b8cb48ce1c628788a2456d21.zip |
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.
Diffstat (limited to 'actionpack')
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 |