match descendents in have_selector/have_xpath blocks

This commit is contained in:
Thomas Jack 2009-04-29 01:33:26 -05:00
parent 481bfe03c0
commit 9a3668be92
3 changed files with 18 additions and 4 deletions

View File

@ -32,7 +32,7 @@ module Webrat
def rexml_matches(stringlike) def rexml_matches(stringlike)
if REXML::Node === stringlike || Array === stringlike if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'//', './') } @query = query.map { |q| q.gsub(%r'^//', './/') }
else else
@query = query @query = query
end end
@ -52,7 +52,7 @@ module Webrat
def nokogiri_matches(stringlike) def nokogiri_matches(stringlike)
if Nokogiri::XML::NodeSet === stringlike if Nokogiri::XML::NodeSet === stringlike
@query = query.gsub(%r'//', './') @query = query.gsub(%r'^//', './/')
else else
@query = query @query = query
end end

View File

@ -13,6 +13,7 @@ describe "have_selector" do
<ul> <ul>
<li>First</li> <li>First</li>
<li>Second</li> <li>Second</li>
<li><a href="http://example.org/">Third</a></li>
</ul> </ul>
</div> </div>
HTML HTML
@ -52,12 +53,12 @@ describe "have_selector" do
describe "specifying counts" do describe "specifying counts" do
it "should be able to specify the number of occurences of the tag" do it "should be able to specify the number of occurences of the tag" do
@body.should have_selector("li", :count => 2) @body.should have_selector("li", :count => 3)
end end
it "should not match if the count is wrong" do it "should not match if the count is wrong" do
lambda { lambda {
@body.should have_selector("li", :count => 3) @body.should have_selector("li", :count => 4)
}.should raise_error(Spec::Expectations::ExpectationNotMetError) }.should raise_error(Spec::Expectations::ExpectationNotMetError)
end end
end end
@ -97,6 +98,12 @@ describe "have_selector" do
n.should have_selector("li", :content => "Second") n.should have_selector("li", :content => "Second")
end end
end end
it "should work with descendants of the matched elements" do
@body.should have_selector("ul") do |n|
n.should have_selector("a", :content => "Third")
end
end
end end
describe "Test::Unit assertions" do describe "Test::Unit assertions" do

View File

@ -13,6 +13,7 @@ describe "have_xpath" do
<ul> <ul>
<li>First</li> <li>First</li>
<li>Second</li> <li>Second</li>
<li><a href="http://example.org">Third</a></li>
</ul> </ul>
</div> </div>
HTML HTML
@ -88,6 +89,12 @@ describe "have_xpath" do
}.should raise_error(Spec::Expectations::ExpectationNotMetError) }.should raise_error(Spec::Expectations::ExpectationNotMetError)
end end
it "should work with descendants of the matched elements" do
@body.should have_xpath("//ul") do |node|
node.should have_xpath("//a[@href='http://example.org']")
end
end
describe 'asserts for xpath' do describe 'asserts for xpath' do
include Test::Unit::Assertions include Test::Unit::Assertions