mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add default_selector into session as well in case DSL isn't being used. Add some specs.
This commit is contained in:
parent
7c528f9b52
commit
2c769c57ee
4 changed files with 58 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
|||
module Capybara
|
||||
class << self
|
||||
attr_writer :default_driver, :current_driver, :javascript_driver
|
||||
attr_writer :default_selector
|
||||
attr_reader :default_selector
|
||||
|
||||
attr_accessor :app
|
||||
|
||||
|
@ -23,15 +23,24 @@ module Capybara
|
|||
end
|
||||
|
||||
def current_session
|
||||
session_pool["#{current_driver}#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
|
||||
session_pool["#{current_driver}#{app.object_id}"] ||= begin
|
||||
session = Capybara::Session.new(current_driver, app)
|
||||
session.default_selector = default_selector if default_selector
|
||||
session
|
||||
end
|
||||
end
|
||||
|
||||
def current_session?
|
||||
session_pool.has_key?("#{current_driver}#{app.object_id}")
|
||||
end
|
||||
|
||||
def reset_sessions!
|
||||
@session_pool = nil
|
||||
end
|
||||
|
||||
def default_selector
|
||||
@default_selector ||= :xpath
|
||||
def default_selector=(selector)
|
||||
@default_selector = selector
|
||||
current_session.default_selector = selector if current_session?
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -12,6 +12,7 @@ class Capybara::Session
|
|||
}
|
||||
|
||||
attr_reader :mode, :app
|
||||
attr_writer :default_selector
|
||||
|
||||
def initialize(mode, app)
|
||||
@mode = mode
|
||||
|
@ -31,6 +32,10 @@ class Capybara::Session
|
|||
end
|
||||
end
|
||||
|
||||
def default_selector
|
||||
@default_selector ||= :xpath
|
||||
end
|
||||
|
||||
def visit(path)
|
||||
driver.visit(path)
|
||||
end
|
||||
|
@ -92,7 +97,7 @@ class Capybara::Session
|
|||
end
|
||||
|
||||
def within(kind, scope=nil)
|
||||
kind, scope = Capybara.default_selector, kind unless scope
|
||||
kind, scope = default_selector, kind unless scope
|
||||
scope = css_to_xpath(scope) if kind == :css
|
||||
raise Capybara::ElementNotFound, "scope '#{scope}' not found on page" if find(scope).empty?
|
||||
scopes.push(scope)
|
||||
|
|
|
@ -11,6 +11,7 @@ describe Capybara do
|
|||
after do
|
||||
Capybara.default_driver = nil
|
||||
Capybara.use_default_driver
|
||||
Capybara.default_selector = nil
|
||||
end
|
||||
|
||||
describe '#default_driver' do
|
||||
|
@ -108,6 +109,20 @@ describe Capybara do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#default_selector' do
|
||||
after do
|
||||
Capybara.default_selector = nil
|
||||
end
|
||||
|
||||
it "should set the selector used in current and future sessions" do
|
||||
Capybara.default_selector.should_not eql(:css)
|
||||
Capybara.default_selector = :css
|
||||
Capybara.current_session.default_selector.should eql(:css)
|
||||
Capybara.reset_sessions!
|
||||
Capybara.current_session.default_selector.should eql(:css)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'the DSL' do
|
||||
before do
|
||||
@session = Capybara
|
||||
|
|
|
@ -543,6 +543,23 @@ shared_examples_for "session" do
|
|||
end
|
||||
end
|
||||
|
||||
context "with the default selector" do
|
||||
it "should use XPath" do
|
||||
lambda {
|
||||
@session.within("//li[contains(., 'With Simple HTML')]") { }
|
||||
}.should_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context "with the default selector set to CSS" do
|
||||
it "should use CSS" do
|
||||
@session.default_selector = :css
|
||||
lambda {
|
||||
@session.within("ul li[contains('With Simple HTML')]") { }
|
||||
}.should_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context "with click_link" do
|
||||
it "should click links in the given scope" do
|
||||
@session.within("//li[contains(.,'With Simple HTML')]") do
|
||||
|
|
Loading…
Reference in a new issue