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:
parent
d3b71f0745
commit
d22b75ead0
2 changed files with 41 additions and 2 deletions
|
@ -64,10 +64,14 @@ class Webcat::Session
|
|||
end
|
||||
|
||||
def has_xpath?(path, options={})
|
||||
results = find(path)
|
||||
if options[:text]
|
||||
results = filter_by_text(results, options[:text])
|
||||
end
|
||||
if options[:count]
|
||||
find(path).size == options[:count]
|
||||
results.size == options[:count]
|
||||
else
|
||||
find(path).size > 0
|
||||
results.size > 0
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -88,6 +92,17 @@ class Webcat::Session
|
|||
|
||||
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
|
||||
scopes.join('')
|
||||
end
|
||||
|
|
|
@ -344,6 +344,18 @@ shared_examples_for "session" do
|
|||
@session.should_not have_xpath("//p//a[@id='doesnotexist']", :count => 1)
|
||||
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
|
||||
|
||||
describe '#has_css?' do
|
||||
|
@ -385,6 +397,18 @@ shared_examples_for "session" do
|
|||
@session.should_not have_css("p a.doesnotexist", :count => 1)
|
||||
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
|
||||
|
||||
describe "#attach_file" do
|
||||
|
|
Loading…
Reference in a new issue