2009-02-16 00:47:12 +00:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
|
|
|
|
|
|
|
describe "have_xpath" do
|
|
|
|
include Webrat::Matchers
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
before(:each) do
|
|
|
|
@body = <<-HTML
|
|
|
|
<div id='main'>
|
|
|
|
<div class='inner'>hello, world!</div>
|
|
|
|
<h2>Welcome "Bryan"</h2>
|
|
|
|
<h3>Welcome 'Bryan'</h3>
|
|
|
|
<h4>Welcome 'Bryan"</h4>
|
|
|
|
<ul>
|
|
|
|
<li>First</li>
|
|
|
|
<li>Second</li>
|
2009-04-29 06:33:26 +00:00
|
|
|
<li><a href="http://example.org">Third</a></li>
|
2009-02-16 00:47:12 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
HTML
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should be able to match an XPATH" do
|
|
|
|
@body.should have_xpath("//div")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should be able to match an XPATH with attributes" do
|
|
|
|
@body.should have_xpath("//div", :class => "inner")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should be able to match an XPATH with content" do
|
|
|
|
@body.should have_xpath("//div", :content => "hello, world!")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should not match an XPATH without content" do
|
|
|
|
@body.should_not have_xpath("//div", :content => "not present")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should be able to match an XPATH with content and class" do
|
|
|
|
@body.should have_xpath("//div", :class => "inner", :content => "hello, world!")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should not match an XPATH with content and wrong class" do
|
|
|
|
@body.should_not have_xpath("//div", :class => "outer", :content => "hello, world!")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should not match an XPATH with wrong content and class" do
|
|
|
|
@body.should_not have_xpath("//div", :class => "inner", :content => "wrong")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-24 01:47:02 +00:00
|
|
|
it "should not match an XPATH with wrong content and wrong class" do
|
|
|
|
@body.should_not have_xpath("//div", :class => "outer", :content => "wrong")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should not match a XPATH that does not exist" do
|
|
|
|
@body.should_not have_xpath("//p")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should be able to loop over all the matched elements" do
|
|
|
|
@body.should have_xpath("//div") do |node|
|
|
|
|
node.first.name.should == "div"
|
|
|
|
end
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should not match if any of the matchers in the block fail" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_xpath("//div") do |node|
|
|
|
|
node.first.name.should == "p"
|
|
|
|
end
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should be able to use #have_xpath in the block" do
|
|
|
|
@body.should have_xpath("//div[@id='main']") do |node|
|
|
|
|
node.should have_xpath("./div[@class='inner']")
|
|
|
|
end
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should convert absolute paths to relative in the block" do
|
|
|
|
@body.should have_xpath("//div[@id='main']") do |node|
|
|
|
|
node.should have_xpath("//div[@class='inner']")
|
|
|
|
end
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should not match any parent tags in the block" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_xpath("//div[@class='inner']") do |node|
|
|
|
|
node.should have_xpath("//div[@id='main']")
|
|
|
|
end
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-05-13 19:52:53 +00:00
|
|
|
it "should match descendants of the matched elements in the block" do
|
2009-04-29 06:33:26 +00:00
|
|
|
@body.should have_xpath("//ul") do |node|
|
|
|
|
node.should have_xpath("//a[@href='http://example.org']")
|
|
|
|
end
|
|
|
|
end
|
2009-05-13 19:52:53 +00:00
|
|
|
|
|
|
|
it "should allow descendant selectors in the block" do
|
|
|
|
@body.should have_xpath("//div[@id='main']") do |node|
|
|
|
|
node.should have_xpath("//ul//a")
|
|
|
|
end
|
|
|
|
end
|
2009-04-29 06:33:26 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe 'asserts for xpath' do
|
|
|
|
include Test::Unit::Assertions
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe "assert_have_xpath" do
|
|
|
|
it "should pass when body contains the selection" do
|
|
|
|
assert_have_xpath("//div")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body doesnt have matching xpath" do
|
|
|
|
lambda {
|
|
|
|
assert_have_xpath("//p")
|
|
|
|
}.should raise_error(Test::Unit::AssertionFailedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "assert_have_no_xpath" do
|
|
|
|
it "should pass when the body doesn't contan the xpath" do
|
|
|
|
assert_have_no_xpath("//p")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body does contain the xpath" do
|
|
|
|
lambda {
|
|
|
|
assert_have_no_xpath("//div")
|
|
|
|
}.should raise_error(Test::Unit::AssertionFailedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|