Add `exact` option
This commit is contained in:
parent
d79413321f
commit
aa3d3e1ead
|
@ -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]
|
||||
|
||||
def initialize(*args)
|
||||
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
||||
|
@ -20,7 +20,13 @@ module Capybara
|
|||
end
|
||||
@selector ||= Selector.all[Capybara.default_selector]
|
||||
|
||||
@xpath = @selector.call(@locator).to_s
|
||||
@xpath = @selector.call(@locator)
|
||||
if @xpath.respond_to?(:to_xpath) and @options[:exact]
|
||||
@xpath = @xpath.to_xpath(:exact)
|
||||
else
|
||||
@xpath = @xpath.to_s
|
||||
end
|
||||
|
||||
|
||||
assert_valid_keys!
|
||||
end
|
||||
|
|
|
@ -113,6 +113,24 @@ 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
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
context "within a scope" do
|
||||
before do
|
||||
@session.visit('/with_scope')
|
||||
|
|
2
xpath
2
xpath
|
@ -1 +1 @@
|
|||
Subproject commit 130b0192a8fbbff6110efc306b33697be66c631e
|
||||
Subproject commit 4c7c3351f970f14635e670239e11f81fffb07717
|
Loading…
Reference in New Issue