Separate DSL out into a module

Otherwise we were including a lot of classes in
the namespace of the object that Capybara was
being included in. This could lead to clashes with
some common class names.
This commit is contained in:
Jonas Nicklas 2011-04-11 06:58:42 +01:00
parent 79ff093727
commit 31f1ac3ccb
8 changed files with 58 additions and 50 deletions

View File

@ -418,7 +418,8 @@ correctly waits for the element to disappear from the page.
== Using the DSL in unsupported testing frameworks
You can mix the DSL into any context by including +Capybara+:
You can mix the DSL into any context by including +Capybara::DSL+:
require 'capybara'
require 'capybara/dsl'
@ -426,7 +427,7 @@ You can mix the DSL into any context by including +Capybara+:
Capybara.default_driver = :culerity
module MyModule
include Capybara
include Capybara::DSL
def login!
within("//form[@id='session']") do

View File

@ -3,7 +3,7 @@ require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec/matchers'
World(Capybara)
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
After do

View File

@ -1,6 +1,11 @@
require 'capybara'
module Capybara
def self.included(base)
base.send(:include, Capybara::DSL)
warn "`include Capybara` is deprecated please use `include Capybara::DSL` instead."
end
class << self
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
@ -98,42 +103,44 @@ module Capybara
end
end
extend(self)
module DSL
##
#
# Shortcut to working in a different session. This is useful when Capybara is included
# in a class or module.
#
def using_session(name, &block)
Capybara.using_session(name, &block)
end
##
#
# Shortcut to accessing the current session. This is useful when Capybara is included in a
# class or module.
#
# class MyClass
# include Capybara
#
# def has_header?
# page.has_css?('h1')
# end
# end
#
# @return [Capybara::Session] The current session object
#
def page
Capybara.current_session
end
Session::DSL_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{method}(*args, &block)
page.#{method}(*args, &block)
end
RUBY
##
#
# Shortcut to working in a different session. This is useful when Capybara is included
# in a class or module.
#
def using_session(name, &block)
Capybara.using_session(name, &block)
end
##
#
# Shortcut to accessing the current session. This is useful when Capybara is included in a
# class or module.
#
# class MyClass
# include Capybara::DSL
#
# def has_header?
# page.has_css?('h1')
# end
# end
#
# @return [Capybara::Session] The current session object
#
def page
Capybara.current_session
end
Session::DSL_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{method}(*args, &block)
page.#{method}(*args, &block)
end
RUBY
end
end
extend(Capybara::DSL)
end

View File

@ -5,20 +5,20 @@ require 'capybara/rspec/matchers'
require 'capybara/rspec/features'
RSpec.configure do |config|
config.include Capybara, :type => :request
config.include Capybara, :type => :acceptance
config.include Capybara::DSL, :type => :request
config.include Capybara::DSL, :type => :acceptance
config.include Capybara::RSpecMatchers, :type => :request
config.include Capybara::RSpecMatchers, :type => :acceptance
# The before and after blocks must run instantaneously, because Capybara
# might not actually be used in all examples where it's included.
config.after do
if self.class.include?(Capybara)
if self.class.include?(Capybara::DSL)
Capybara.reset_sessions!
Capybara.use_default_driver
end
end
config.before do
if self.class.include?(Capybara)
if self.class.include?(Capybara::DSL)
Capybara.current_driver = Capybara.javascript_driver if example.metadata[:js]
Capybara.current_driver = example.metadata[:driver] if example.metadata[:driver]
end

View File

@ -41,7 +41,7 @@ module Capybara
:body, :html, :current_url, :current_host, :evaluate_script, :source,
:visit, :wait_until, :within, :within_fieldset, :within_table,
:within_frame, :within_window, :current_path, :save_page,
:save_and_open_page
:save_and_open_page, :reset!, :app
]
DSL_METHODS = NODE_METHODS + SESSION_METHODS

View File

@ -2,7 +2,7 @@ require 'spec_helper'
require 'capybara/dsl'
describe Capybara do
describe Capybara::DSL do
before do
Capybara.app = TestApp
@ -170,7 +170,7 @@ describe Capybara do
describe 'the DSL' do
before do
@session = Capybara
@session = Class.new { include Capybara::DSL }.new
end
it_should_behave_like "session"
@ -178,7 +178,7 @@ describe Capybara do
it "should be possible to include it in another class" do
klass = Class.new do
include Capybara
include Capybara::DSL
end
foo = klass.new
foo.visit('/with_html')
@ -188,7 +188,7 @@ describe Capybara do
it "should provide a 'page' shortcut for more expressive tests" do
klass = Class.new do
include Capybara
include Capybara::DSL
end
foo = klass.new
foo.page.visit('/with_html')
@ -198,7 +198,7 @@ describe Capybara do
it "should provide an 'using_session' shortcut" do
klass = Class.new do
include Capybara
include Capybara::DSL
end
Capybara.should_receive(:using_session).with(:name)
foo = klass.new

View File

@ -5,7 +5,7 @@ require 'capybara/rspec/matchers'
Capybara.app = TestApp
describe Capybara::RSpecMatchers do
include Capybara
include Capybara::DSL
include Capybara::RSpecMatchers
describe "have_css matcher" do

View File

@ -64,7 +64,7 @@ describe Capybara do
string.find('//h1').path.should == '/html/body/div/div[1]/h1'
end
it "allows finding elements and extracting the path" do
it "allows finding elements and extracting the value" do
string.find('//input').value.should == 'bar'
string.find('//select').value.should == 'Capybara'
end