require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") describe "have_xpath" do include Webrat::Matchers before(:each) do @body = <<-HTML
hello, world!

Welcome "Bryan"

Welcome 'Bryan'

Welcome 'Bryan"

HTML end it "should be able to match an XPATH" do @body.should have_xpath("//div") end it "should not match a XPATH that does not exist" do @body.should_not have_xpath("//p") end 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 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 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 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 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 describe 'asserts for xpath' do include Test::Unit::Assertions before(:each) do should_receive(:response_body).and_return @body require 'test/unit' end 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