1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Added :text option to has_xpath and has_css

This emulates the same option on Rails' have_tag
This commit is contained in:
Theo Hultberg and Jonas Nicklas 2009-11-16 20:13:06 +01:00 committed by Jonas Nicklas
parent d3b71f0745
commit d22b75ead0
2 changed files with 41 additions and 2 deletions

View file

@ -64,10 +64,14 @@ class Webcat::Session
end end
def has_xpath?(path, options={}) def has_xpath?(path, options={})
results = find(path)
if options[:text]
results = filter_by_text(results, options[:text])
end
if options[:count] if options[:count]
find(path).size == options[:count] results.size == options[:count]
else else
find(path).size > 0 results.size > 0
end end
end end
@ -88,6 +92,17 @@ class Webcat::Session
private private
def filter_by_text(nodes, text)
nodes.select do |node|
case text
when String
node.text.include?(text)
when Regexp
node.text =~ text
end
end
end
def current_scope def current_scope
scopes.join('') scopes.join('')
end end

View file

@ -344,6 +344,18 @@ shared_examples_for "session" do
@session.should_not have_xpath("//p//a[@id='doesnotexist']", :count => 1) @session.should_not have_xpath("//p//a[@id='doesnotexist']", :count => 1)
end end
end end
context "with text" do
it "should discard all matches where the given string is not contained" do
@session.should have_xpath("//p//a", :text => "Redirect", :count => 1)
@session.should_not have_xpath("//p", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is not matched" do
@session.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
@session.should_not have_xpath("//p//a", :text => /Red$/)
end
end
end end
describe '#has_css?' do describe '#has_css?' do
@ -385,6 +397,18 @@ shared_examples_for "session" do
@session.should_not have_css("p a.doesnotexist", :count => 1) @session.should_not have_css("p a.doesnotexist", :count => 1)
end end
end end
context "with text" do
it "should discard all matches where the given string is not contained" do
@session.should have_css("p a", :text => "Redirect", :count => 1)
@session.should_not have_css("p a", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is not matched" do
@session.should have_css("p a", :text => /re[dab]i/i, :count => 1)
@session.should_not have_css("p a", :text => /Red$/)
end
end
end end
describe "#attach_file" do describe "#attach_file" do