2016-12-15 12:04:01 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2016-12-15 12:04:01 -05:00
|
|
|
require 'forwardable'
|
|
|
|
require 'capybara/session/config'
|
|
|
|
|
|
|
|
module Capybara
|
|
|
|
class Config
|
|
|
|
extend Forwardable
|
|
|
|
|
2019-04-05 14:32:30 -04:00
|
|
|
OPTIONS = %i[app reuse_server threadsafe server default_driver javascript_driver allow_gumbo].freeze
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
attr_accessor :app
|
|
|
|
attr_reader :reuse_server, :threadsafe
|
|
|
|
attr_reader :session_options
|
|
|
|
attr_writer :default_driver, :javascript_driver
|
2018-12-06 17:23:35 -05:00
|
|
|
attr_accessor :allow_gumbo
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
SessionConfig::OPTIONS.each do |method|
|
|
|
|
def_delegators :session_options, method, "#{method}="
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@session_options = Capybara::SessionConfig.new
|
2018-05-11 15:37:45 -04:00
|
|
|
@javascript_driver = nil
|
2016-12-15 12:04:01 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
attr_writer :reuse_server
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
def threadsafe=(bool)
|
2019-10-15 21:02:35 -04:00
|
|
|
if (bool != threadsafe) && Session.instance_created?
|
|
|
|
raise 'Threadsafe setting cannot be changed once a session is created'
|
|
|
|
end
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2016-12-15 12:04:01 -05:00
|
|
|
@threadsafe = bool
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Return the proc that Capybara will call to run the Rack application.
|
|
|
|
# The block returned receives a rack app, port, and host/ip and should run a Rack handler
|
2018-01-13 16:06:03 -05:00
|
|
|
# By default, Capybara will try to use puma.
|
2016-12-15 12:04:01 -05:00
|
|
|
#
|
2018-01-13 16:06:03 -05:00
|
|
|
attr_reader :server
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Set the server to use.
|
|
|
|
#
|
|
|
|
# Capybara.server = :webrick
|
2017-08-01 19:50:05 -04:00
|
|
|
# Capybara.server = :puma, { Silent: true }
|
2016-12-15 12:04:01 -05:00
|
|
|
#
|
2017-08-01 19:50:05 -04:00
|
|
|
# @overload server=(name)
|
|
|
|
# @param [Symbol] name Name of the server type to use
|
|
|
|
# @overload server=([name, options])
|
|
|
|
# @param [Symbol] name Name of the server type to use
|
|
|
|
# @param [Hash] options Options to pass to the server block
|
2016-12-15 12:04:01 -05:00
|
|
|
# @see register_server
|
|
|
|
#
|
|
|
|
def server=(name)
|
2017-08-01 19:50:05 -04:00
|
|
|
name, options = *name if name.is_a? Array
|
2018-04-06 13:17:33 -04:00
|
|
|
@server = if name.respond_to? :call
|
|
|
|
name
|
|
|
|
elsif options
|
2019-11-28 17:41:13 -05:00
|
|
|
proc { |app, port, host| Capybara.servers[name.to_sym].call(app, port, host, **options) }
|
2016-12-15 12:04:01 -05:00
|
|
|
else
|
2016-08-17 16:21:21 -04:00
|
|
|
Capybara.servers[name.to_sym]
|
2016-12-15 12:04:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the driver to use by default
|
|
|
|
#
|
|
|
|
def default_driver
|
|
|
|
@default_driver || :rack_test
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the driver used when JavaScript is needed
|
|
|
|
#
|
|
|
|
def javascript_driver
|
|
|
|
@javascript_driver || :selenium
|
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
def deprecate(method, alternate_method, once = false)
|
2016-12-15 12:04:01 -05:00
|
|
|
@deprecation_notified ||= {}
|
2019-10-15 21:02:35 -04:00
|
|
|
unless once && @deprecation_notified[method]
|
|
|
|
warn "DEPRECATED: ##{method} is deprecated, please use ##{alternate_method} instead"
|
|
|
|
end
|
2018-01-09 17:05:50 -05:00
|
|
|
@deprecation_notified[method] = true
|
2016-12-15 12:04:01 -05:00
|
|
|
end
|
|
|
|
end
|
2018-01-09 17:05:50 -05:00
|
|
|
end
|