Merge branch 'capy21'

Conflicts:
	spec/basic_node_spec.rb
This commit is contained in:
Jonas Nicklas 2013-02-15 22:53:15 +01:00
commit e330e88a5e
13 changed files with 275 additions and 49 deletions

View File

@ -16,7 +16,7 @@ module Capybara
class << self
attr_accessor :asset_root, :app_host, :run_server, :default_host, :always_include_port
attr_accessor :server_host, :server_port
attr_accessor :server_host, :server_port, :exact, :match
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
attr_accessor :save_and_open_page_path, :automatic_reload
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
@ -344,6 +344,8 @@ Capybara.configure do |config|
config.ignore_hidden_elements = true
config.default_host = "http://www.example.com"
config.automatic_reload = true
config.match = :smart
config.exact = false
end
Capybara.register_driver :rack_test do |app|

View File

@ -23,7 +23,22 @@ module Capybara
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires
#
def find(*args)
synchronize { all(*args).find! }.tap(&:allow_reload!)
synchronize do
query = Capybara::Query.new(*args)
if query.match == :smart or query.match == :prefer_exact
result = resolve_query(query, true)
result = resolve_query(query, false) if result.size == 0 and not query.exact
else
result = resolve_query(query)
end
if query.match == :one or query.match == :smart and result.size > 1
raise Capybara::Ambiguous.new("Ambiguous match, found #{result.size} elements matching #{query.description}")
end
if result.size == 0
raise Capybara::ElementNotFound.new("Unable to find #{query.description}")
end
result.first
end.tap(&:allow_reload!)
end
##
@ -108,13 +123,7 @@ module Capybara
# @return [Capybara::Result] A collection of found elements
#
def all(*args)
query = Capybara::Query.new(*args)
elements = synchronize do
base.find(query.xpath).map do |node|
Capybara::Node::Element.new(session, node, self, query)
end
end
Capybara::Result.new(elements, query)
resolve_query(Capybara::Query.new(*args))
end
##
@ -131,6 +140,17 @@ module Capybara
def first(*args)
all(*args).first
end
private
def resolve_query(query, exact=nil)
elements = synchronize do
base.find(query.xpath(exact)).map do |node|
Capybara::Node::Element.new(session, node, self, query)
end
end
Capybara::Result.new(elements, query)
end
end
end
end

View File

@ -139,9 +139,10 @@ module Capybara
yield # simple nodes don't need to wait
end
def all(*args)
query = Capybara::Query.new(*args)
elements = native.xpath(query.xpath).map do |node|
private
def resolve_query(query, exact=nil)
elements = native.xpath(query.xpath(exact)).map do |node|
self.class.new(node)
end
Capybara::Result.new(elements, query)

View File

@ -2,7 +2,7 @@ module Capybara
class Query
attr_accessor :selector, :locator, :options, :xpath, :find, :negative
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum]
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match]
def initialize(*args)
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
@ -11,6 +11,14 @@ module Capybara
@options[:visible] = Capybara.ignore_hidden_elements
end
unless options.has_key?(:exact)
@options[:exact] = Capybara.exact
end
unless options.has_key?(:match)
@options[:match] = Capybara.match
end
if args[0].is_a?(Symbol)
@selector = Selector.all[args[0]]
@locator = args[1]
@ -20,8 +28,7 @@ module Capybara
end
@selector ||= Selector.all[Capybara.default_selector]
@xpath = @selector.call(@locator).to_s
@xpath = @selector.call(@locator)
assert_valid_keys!
end
@ -63,6 +70,24 @@ module Capybara
end
end
def exact
@options[:exact]
end
def match
@options[:match]
end
def xpath(exact=nil)
exact = @options[:exact] if exact == nil
if @xpath.respond_to?(:to_xpath) and exact
@xpath.to_xpath(:exact)
else
@xpath.to_s
end
end
private
def assert_valid_keys!

View File

@ -35,19 +35,6 @@ module Capybara
@query.matches_count?(@result.size)
end
def find!
raise find_error if @result.size != 1
@result.first
end
def find_error
if @result.size == 0
Capybara::ElementNotFound.new("Unable to find #{@query.description}")
elsif @result.size > 1
Capybara::Ambiguous.new("Ambiguous match, found #{size} elements matching #{@query.description}")
end
end
def failure_message
message = if @query.options[:count]
"expected #{@query.description} to be found #{@query.options[:count]} #{declension("time", "times", @query.options[:count])}"

View File

@ -48,22 +48,23 @@ Capybara::SpecHelper.spec "#all" do
@session.all('h1').first.text.should == 'This is a test'
@session.all("input[id='test_field']").first[:value].should == 'monkey'
end
after { Capybara.default_selector = :xpath }
end
context "with visible filter" do
after { Capybara.ignore_hidden_elements = true }
it "should only find visible nodes" do
@session.all(:css, "a.simple").should have(1).elements
Capybara.ignore_hidden_elements = false
@session.all(:css, "a.simple").should have(2).elements
it "should only find visible nodes when true" do
@session.all(:css, "a.simple", :visible => true).should have(1).elements
end
it "should only find invisible nodes" do
Capybara.ignore_hidden_elements = true
it "should find nodes regardless of whether they are invisible when false" do
@session.all(:css, "a.simple", :visible => false).should have(2).elements
end
it "should default to Capybara.ignore_hidden_elements" do
Capybara.ignore_hidden_elements = true
@session.all(:css, "a.simple").should have(1).elements
Capybara.ignore_hidden_elements = false
@session.all(:css, "a.simple").should have(2).elements
end
end
context "within a scope" do

View File

@ -5,7 +5,6 @@ Capybara::SpecHelper.spec '#find_by_id' do
it "should find any element by id" do
@session.find_by_id('red').tag_name.should == 'a'
@session.find_by_id('hidden_via_ancestor').tag_name.should == 'div'
end
it "casts to string" do

View File

@ -113,6 +113,179 @@ Capybara::SpecHelper.spec '#find' do
@session.find(@xpath).value.should == 'John'
end
context "with :exact option" do
it "matches exactly when true" do
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("test_field")], :exact => true).value.should == "monkey"
expect do
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")], :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
it "matches loosely when false" do
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("test_field")], :exact => false).value.should == "monkey"
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")], :exact => false).value.should == "monkey"
end
it "defaults to `Capybara.exact`" do
Capybara.exact = true
expect do
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")])
end.to raise_error(Capybara::ElementNotFound)
Capybara.exact = false
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")])
end
end
context "with :match option" do
context "when set to `one`" do
it "raises an error when multiple matches exist" do
expect do
@session.find(:css, ".multiple", :match => :one)
end.to raise_error(Capybara::Ambiguous)
end
it "raises an error even if there the match is exact and the others are inexact" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :exact => false, :match => :one)
end.to raise_error(Capybara::Ambiguous)
end
it "returns the element if there is only one" do
@session.find(:css, ".singular", :match => :one).text.should == "singular"
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :any)
end.to raise_error(Capybara::ElementNotFound)
end
end
context "when set to `any`" do
it "returns the first matched element" do
@session.find(:css, ".multiple", :match => :any).text.should == "multiple one"
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :any)
end.to raise_error(Capybara::ElementNotFound)
end
end
context "when set to `smart`" do
context "and `exact` set to `false`" do
it "raises an error when there are multiple exact matches" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :smart, :exact => false)
end.to raise_error(Capybara::Ambiguous)
end
it "finds a single exact match when there also are inexact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :smart, :exact => false)
result.text.should == "almost singular"
end
it "raises an error when there are multiple inexact matches" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :smart, :exact => false)
end.to raise_error(Capybara::Ambiguous)
end
it "finds a single inexact match" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :smart, :exact => false)
result.text.should == "almost singular but not quite"
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :smart, :exact => false)
end.to raise_error(Capybara::ElementNotFound)
end
end
context "with `exact` set to `true`" do
it "raises an error when there are multiple exact matches" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :smart, :exact => true)
end.to raise_error(Capybara::Ambiguous)
end
it "finds a single exact match when there also are inexact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :smart, :exact => true)
result.text.should == "almost singular"
end
it "raises an error when there are multiple inexact matches" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :smart, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
it "raises an error when there is a single inexact matches" do
expect do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :smart, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :smart, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end
context "when set to `prefer_exact`" do
context "and `exact` set to `false`" do
it "picks the first one when there are multiple exact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :prefer_exact, :exact => false)
result.text.should == "multiple one"
end
it "finds a single exact match when there also are inexact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :prefer_exact, :exact => false)
result.text.should == "almost singular"
end
it "picks the first one when there are multiple inexact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :prefer_exact, :exact => false)
result.text.should == "almost singular but not quite"
end
it "finds a single inexact match" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :prefer_exact, :exact => false)
result.text.should == "almost singular but not quite"
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :prefer_exact, :exact => false)
end.to raise_error(Capybara::ElementNotFound)
end
end
context "with `exact` set to `true`" do
it "picks the first one when there are multiple exact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :prefer_exact, :exact => true)
result.text.should == "multiple one"
end
it "finds a single exact match when there also are inexact matches" do
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :prefer_exact, :exact => true)
result.text.should == "almost singular"
end
it "raises an error if there are multiple inexact matches" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :prefer_exact, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
it "raises an error if there is a single inexact match" do
expect do
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :prefer_exact, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
it "raises an error if there is no match" do
expect do
@session.find(:css, ".does-not-exist", :match => :prefer_exact, :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end
it "defaults to `Capybara.match`" do
Capybara.match = :one
expect do
@session.find(:css, ".multiple")
end.to raise_error(Capybara::Ambiguous)
Capybara.match = :any
@session.find(:css, ".multiple").text.should == "multiple one"
end
end
context "within a scope" do
before do
@session.visit('/with_scope')

View File

@ -38,22 +38,24 @@ Capybara::SpecHelper.spec '#first' do
@session.first('h1').text.should == 'This is a test'
@session.first("input[id='test_field']")[:value].should == 'monkey'
end
after { Capybara.default_selector = :xpath }
end
context "with visible filter" do
after { Capybara.ignore_hidden_elements = false }
it "should only find visible nodes if true given" do
@session.first(:css, "a#invisible").should_not be_nil
it "should only find visible nodes when true" do
@session.first(:css, "a#invisible", :visible => true).should be_nil
Capybara.ignore_hidden_elements = true
@session.first(:css, "a#invisible").should be_nil
end
it "should include invisible nodes if false given" do
Capybara.ignore_hidden_elements = true
it "should find nodes regardless of whether they are invisible when false" do
@session.first(:css, "a#invisible", :visible => false).should_not be_nil
@session.first(:css, "a", :visible => false).should_not be_nil
end
it "should default to Capybara.ignore_hidden_elements" do
Capybara.ignore_hidden_elements = true
@session.first(:css, "a#invisible").should be_nil
Capybara.ignore_hidden_elements = false
@session.first(:css, "a#invisible").should_not be_nil
@session.first(:css, "a").should_not be_nil
end
end

View File

@ -91,6 +91,7 @@ Capybara::SpecHelper.spec "node" do
describe "#visible?" do
it "should extract node visibility" do
Capybara.ignore_hidden_elements = false
@session.first('//a').should be_visible
@session.find('//div[@id="hidden"]').should_not be_visible

View File

@ -19,6 +19,9 @@ module Capybara
Capybara.app_host = nil
Capybara.default_selector = :xpath
Capybara.default_wait_time = 1
Capybara.ignore_hidden_elements = true
Capybara.exact = false
Capybara.match = :smart
end
def filter(requires, metadata)

View File

@ -81,3 +81,11 @@ banana</textarea>
<li id="john_monkey">Monkey John</li>
<li id="paul_monkey">Monkey Paul</li>
</ul>
<div>
<div class="singular">singular</div>
<div class="multiple">multiple one</div>
<div class="multiple">multiple two</div>
<div class="almost_singular but_not_quite">almost singular but not quite</div>
<div class="almost_singular">almost singular</div>
</div>

View File

@ -15,7 +15,7 @@ describe Capybara do
<input type="text" name="meh"/>
</form>
<div id="footer" style="display: none">
<div id="footer">
<p>c2010</p>
<p>Jonas Nicklas</p>
<input type="text" name="foo" value="bar"/>
@ -25,6 +25,10 @@ describe Capybara do
</select>
</div>
<div id="hidden" style="display: none">
<p id="secret">Secret</p>
</div>
<section>
<div class="subsection"></div>
</section>
@ -61,8 +65,8 @@ describe Capybara do
end
it "allows finding only visible nodes" do
string.all('//p', :text => 'c2010', :visible => true).should be_empty
string.all('//p', :text => 'c2010', :visible => false).should have(1).element
string.all(:css, '#secret', :visible => true).should be_empty
string.all(:css, '#secret', :visible => false).should have(1).element
end
it "allows finding elements and extracting text from them" do
@ -88,7 +92,7 @@ describe Capybara do
it "allows finding elements and checking if they are visible" do
string.find('//h1').should be_visible
string.find('//div/input').should_not be_visible
string.find(:css, "#secret", :visible => false).should_not be_visible
end
it "allows finding elements and checking if they are disabled" do