Permalink
Browse files

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.
  • Loading branch information...
eileencodes committed Oct 2, 2016
1 parent e9127f7 commit c83e6d36dd13baa8b8cb48ce1c628788a2456d21
@@ -33,7 +33,7 @@ class << self
# # => 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
@@ -1,3 +1,5 @@
require 'system_testing/driver_adapters/web_server'
module SystemTesting
module DriverAdapters
# == CapybaraDriver for System Testing
@@ -41,21 +43,41 @@ module DriverAdapters
# 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
@@ -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 DriverAdapters
# 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 @@ def registration
def setup
set_server
set_driver
set_port
set_driver
end
def register_browser_driver
@@ -89,42 +90,9 @@ def register_browser_driver
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
@@ -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
@@ -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

0 comments on commit c83e6d3

Please sign in to comment.