From 7fc12fd78ea3bf642236cf62315ec73f4272a567 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Thu, 29 Jul 2010 15:20:11 +0200 Subject: [PATCH] Drivers are now manually registered --- lib/capybara.rb | 32 ++++++++++++++++++++++++++++++++ lib/capybara/session.rb | 12 ++++++------ spec/capybara_spec.rb | 13 ++++++++++++- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/lib/capybara.rb b/lib/capybara.rb index 9b30c00b..92f237ba 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -45,6 +45,27 @@ module Capybara def configure yield self end + + ## + # + # Register a new driver for Capybara. + # + # Capybara.register_driver :rack_test do |app| + # Capybara::Driver::RackTest.new(app) + # end + # + # @param [Symbol] name The name of the new driver + # @yield [app] This block takes a rack app and returns a Capybara driver + # @yieldparam [] app The rack application that this driver runs agains. May be nil. + # @yieldreturn [Capybara::Driver::Base] A Capybara driver instance + # + def register_driver(name, &block) + drivers[name] = block + end + + def drivers + @drivers ||= {} + end end autoload :Server, 'capybara/server' @@ -72,3 +93,14 @@ Capybara.configure do |config| config.ignore_hidden_elements = false end +Capybara.register_driver :rack_test do |app| + Capybara::Driver::RackTest.new(app) +end + +Capybara.register_driver :culerity do |app| + Capybara::Driver::Culerity.new(app) +end + +Capybara.register_driver :selenium do |app| + Capybara::Driver::Selenium.new(app) +end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index f150a5c7..9faaf48d 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -43,12 +43,12 @@ module Capybara end def driver - @driver ||= begin - string = mode.to_s - string.gsub!(%r{(^.)|(_.)}) { |m| m[m.length-1,1].upcase } - Capybara::Driver.const_get(string.to_sym).new(app) - rescue NameError - raise Capybara::DriverNotFoundError, "no driver called #{mode} was found" + @driver ||= begin + unless Capybara.drivers.has_key?(mode) + other_drivers = Capybara.drivers.keys.map { |key| key.inspect } + raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}" + end + Capybara.drivers[mode].call(app) end end diff --git a/spec/capybara_spec.rb b/spec/capybara_spec.rb index f50c2254..80e2fe9a 100644 --- a/spec/capybara_spec.rb +++ b/spec/capybara_spec.rb @@ -14,5 +14,16 @@ describe Capybara do Capybara.default_wait_time.should == 5 end end + + describe '.register_driver' do + it "should add a new driver" do + Capybara.register_driver :schmoo do |app| + Capybara::Driver::RackTest.new(app) + end + session = Capybara::Session.new(:schmoo, TestApp) + session.visit('/') + session.body.should include("Hello world!") + end + end -end \ No newline at end of file +end