1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Get control of driver/server registrations

This commit is contained in:
Thomas Walpole 2020-05-25 14:49:51 -07:00
parent 3842679ecf
commit 6fcd43aec8
5 changed files with 53 additions and 8 deletions

View file

@ -5,6 +5,7 @@ require 'nokogiri'
require 'xpath' require 'xpath'
require 'forwardable' require 'forwardable'
require 'capybara/config' require 'capybara/config'
require 'capybara/registration_container'
module Capybara module Capybara
class CapybaraError < StandardError; end class CapybaraError < StandardError; end
@ -126,7 +127,7 @@ module Capybara
# @yieldreturn [Capybara::Driver::Base] A Capybara driver instance # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance
# #
def register_driver(name, &block) def register_driver(name, &block)
drivers[name] = block drivers.send(:register, name, block)
end end
## ##
@ -145,7 +146,7 @@ module Capybara
# @yieldparam host The host/ip to bind to # @yieldparam host The host/ip to bind to
# #
def register_server(name, &block) def register_server(name, &block)
servers[name.to_sym] = block servers.send(:register, name.to_sym, block)
end end
## ##
@ -199,11 +200,11 @@ module Capybara
end end
def drivers def drivers
@drivers ||= {} @drivers ||= RegistrationContainer.new
end end
def servers def servers
@servers ||= {} @servers ||= RegistrationContainer.new
end end
# Wraps the given string, which should contain an HTML document or fragment # Wraps the given string, which should contain an HTML document or fragment

View file

@ -22,6 +22,6 @@ end
Before do |scenario| Before do |scenario|
scenario.source_tag_names.each do |tag| scenario.source_tag_names.each do |tag|
driver_name = tag.sub(/^@/, '').to_sym driver_name = tag.sub(/^@/, '').to_sym
Capybara.current_driver = driver_name if Capybara.drivers.key?(driver_name) Capybara.current_driver = driver_name if Capybara.drivers[driver_name]
end end
end end

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Capybara
# @api private
class RegistrationContainer
def names
@registered.keys
end
def [](name)
@registered[name]
end
def []=(name, value)
warn 'DEPRECATED: Directly setting drivers/servers is deprecated, please use Capybara.register_driver/register_server instead'
@registered[name] = value
end
def method_missing(method_name, *args, **options, &block)
if @registered.respond_to?(method_name)
warn "DEPRECATED: Calling '#{method_name}' on the drivers/servers container is deprecated without replacement"
# RUBY 2.6 will send an empty hash rather than nothing with **options so fix that
return @registered.public_send(method_name, *args, &block) if options.empty?
return @registered.public_send(method_name, *args, **options, &block)
end
super
end
def respond_to_missing?(method_name, include_private = false)
@registered.respond_to?(method_name) || super
end
private
def initialize
@registered = {}
end
def register(name, block)
@registered[name] = block
end
end
end

View file

@ -97,8 +97,8 @@ module Capybara
def driver def driver
@driver ||= begin @driver ||= begin
unless Capybara.drivers.key?(mode) unless Capybara.drivers[mode]
other_drivers = Capybara.drivers.keys.map(&:inspect) other_drivers = Capybara.drivers.names.map(&:inspect)
raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}" raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}"
end end
driver = Capybara.drivers[mode].call(app) driver = Capybara.drivers[mode].call(app)

View file

@ -32,7 +32,7 @@ RSpec.describe Capybara do
# do nothing # do nothing
end end
expect(described_class.servers).to have_key(:blob) expect(described_class.servers[:blob]).to be_truthy
end end
end end