diff --git a/lib/webcat/session.rb b/lib/webcat/session.rb index bb151c99..a8a3af5e 100644 --- a/lib/webcat/session.rb +++ b/lib/webcat/session.rb @@ -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 diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 09ce62f9..10ace932 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -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