Skip to content

Commit

Permalink
Add prefer_exact match strategy which behaves like Capybara 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Feb 15, 2013
1 parent e898cc2 commit 4a617bc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/capybara/node/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Finders
def find(*args)
synchronize do
query = Capybara::Query.new(*args)
if query.match == :smart
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
Expand Down
62 changes: 62 additions & 0 deletions lib/capybara/spec/session/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
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
Expand All @@ -211,6 +216,63 @@
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

Expand Down

0 comments on commit 4a617bc

Please sign in to comment.