2008-11-06 19:05:52 +00:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
|
|
|
|
|
|
|
describe Webrat::Matchers do
|
|
|
|
include Webrat::Matchers
|
2008-11-06 22:24:07 +00:00
|
|
|
include Webrat::HaveTagMatcher
|
2008-11-06 19:05:52 +00:00
|
|
|
|
|
|
|
before(:each) do
|
2008-11-28 05:34:35 +00:00
|
|
|
@body = <<-HTML
|
|
|
|
<div id='main'>
|
|
|
|
<div class='inner'>hello, world!</div>
|
|
|
|
<ul>
|
|
|
|
<li>First</li>
|
|
|
|
<li>Second</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
HTML
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
|
2008-11-09 02:34:46 +00:00
|
|
|
describe "#have_xpath" do
|
|
|
|
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") { |node| node.first.name.should == "div" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match of any of the matchers in the block fail" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_xpath("//div") { |node| node.first.name.should == "p" }
|
|
|
|
}.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']") { |node| node.should have_xpath("./div[@class='inner']") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should convert absolute paths to relative in the block" do
|
|
|
|
@body.should have_xpath("//div[@id='main']") { |node| node.should have_xpath("//div[@class='inner']") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match any parent tags in the block" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_xpath("//div[@class='inner']") { |node| node.should have_xpath("//div[@id='main']") }
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
|
|
|
|
2009-01-05 18:23:48 +00:00
|
|
|
describe 'asserts for xpath' do
|
2009-01-14 04:24:55 +00:00
|
|
|
include Test::Unit::Assertions
|
2009-01-05 18:23:48 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2009-01-05 18:58:40 +00:00
|
|
|
describe "assert_have_xpath" do
|
2009-01-05 18:23:48 +00:00
|
|
|
it "should pass when body contains the selection" do
|
2009-01-05 18:58:40 +00:00
|
|
|
assert_have_xpath("//div")
|
2009-01-05 18:23:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body doesnt have matching xpath" do
|
2009-01-05 18:58:40 +00:00
|
|
|
lambda {assert_have_xpath("//p")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-05 18:23:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-01-05 18:58:40 +00:00
|
|
|
describe "assert_have_no_xpath" do
|
2009-01-05 18:23:48 +00:00
|
|
|
it "should pass when the body doesn't contan the xpath" do
|
2009-01-05 18:58:40 +00:00
|
|
|
assert_have_no_xpath("//p")
|
2009-01-05 18:23:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body does contain the xpath" do
|
2009-01-05 18:58:40 +00:00
|
|
|
lambda {assert_have_no_xpath("//div")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-05 18:23:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-09 02:34:46 +00:00
|
|
|
end
|
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
describe "#have_selector" do
|
|
|
|
|
|
|
|
it "should be able to match a CSS selector" do
|
|
|
|
@body.should have_selector("div")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match a CSS selector that does not exist" do
|
|
|
|
@body.should_not have_selector("p")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to loop over all the matched elements" do
|
2008-11-09 02:34:46 +00:00
|
|
|
@body.should have_selector("div") { |node| node.first.name.should == "div" }
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match of any of the matchers in the block fail" do
|
|
|
|
lambda {
|
2008-11-09 02:34:46 +00:00
|
|
|
@body.should have_selector("div") { |node| node.first.name.should == "p" }
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to use #have_selector in the block" do
|
|
|
|
@body.should have_selector("#main") { |node| node.should have_selector(".inner") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match any parent tags in the block" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_selector(".inner") { |node| node.should have_selector("#main") }
|
2008-11-06 19:05:52 +00:00
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
|
|
|
|
2009-01-05 00:00:18 +00:00
|
|
|
describe "asserts for selector," do
|
2009-01-14 04:24:55 +00:00
|
|
|
include Test::Unit::Assertions
|
2009-01-05 00:00:18 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2009-01-05 18:59:56 +00:00
|
|
|
describe "assert_have_selector" do
|
2009-01-05 00:00:18 +00:00
|
|
|
it "should pass when body contains the selection" do
|
2009-01-05 18:59:56 +00:00
|
|
|
assert_have_selector("div")
|
2009-01-05 00:00:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body doesnt have matching selection" do
|
2009-01-05 18:59:56 +00:00
|
|
|
lambda {assert_have_selector("p")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-05 00:00:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-01-05 18:59:56 +00:00
|
|
|
describe "assert_have_not_selector" do
|
2009-01-05 00:00:18 +00:00
|
|
|
it "should pass when the body doesn't contan the selection" do
|
2009-01-05 18:59:56 +00:00
|
|
|
assert_have_no_selector("p")
|
2009-01-05 00:00:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body does contain the selection" do
|
2009-01-05 18:59:56 +00:00
|
|
|
lambda {assert_have_no_selector("div")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-05 00:00:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#have_tag" do
|
|
|
|
|
|
|
|
it "should be able to match a tag" do
|
|
|
|
@body.should have_tag("div")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match the tag when it should not match" do
|
|
|
|
@body.should_not have_tag("p")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to specify the content of the tag" do
|
|
|
|
@body.should have_tag("div", :content => "hello, world!")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to specify the attributes of the tag" do
|
|
|
|
@body.should have_tag("div", :class => "inner")
|
|
|
|
end
|
|
|
|
|
2008-11-09 02:34:46 +00:00
|
|
|
it "should be able to loop over all the matched elements" do
|
|
|
|
@body.should have_tag("div") { |node| node.first.name.should == "div" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match of any of the matchers in the block fail" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_tag("div") { |node| node.first.name.should == "p" }
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to use #have_tag in the block" do
|
|
|
|
@body.should have_tag("div", :id => "main") { |node| node.should have_tag("div", :class => "inner") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not match any parent tags in the block" do
|
|
|
|
lambda {
|
|
|
|
@body.should have_tag("div", :class => "inner") { |node| node.should have_tag("div", :id => "main") }
|
|
|
|
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should work with items that have multiple child nodes" do
|
|
|
|
@body.should have_tag("ul") { |n|
|
|
|
|
n.should have_tag("li", :content => "First")
|
|
|
|
n.should have_tag("li", :content => "Second")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-04 23:42:54 +00:00
|
|
|
describe "asserts for tags," do
|
2009-01-14 04:24:55 +00:00
|
|
|
include Test::Unit::Assertions
|
2009-01-04 23:42:54 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2009-01-05 19:03:08 +00:00
|
|
|
describe "assert_have_tag" do
|
2009-01-04 23:42:54 +00:00
|
|
|
it "should pass when body contains the tag" do
|
2009-01-05 19:03:08 +00:00
|
|
|
assert_have_tag("div")
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should pass when finding with additional selectors" do
|
2009-01-05 19:03:08 +00:00
|
|
|
assert_have_tag("div", :class => "inner")
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it "should throw an exception when the body doesnt have matching tag" do
|
2009-01-05 19:03:08 +00:00
|
|
|
lambda {assert_have_tag("p")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body doens't have a tag matching the additional selector" do
|
2009-01-05 19:03:08 +00:00
|
|
|
lambda {assert_have_tag("div", :class => "nope")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-05 19:03:08 +00:00
|
|
|
describe "assert_have_no_tag" do
|
2009-01-04 23:42:54 +00:00
|
|
|
it "should pass when the body doesn't contan the tag" do
|
2009-01-05 19:03:08 +00:00
|
|
|
assert_have_no_tag("p")
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should pass when the body doesn't contain the tag due to additional selectors missing" do
|
2009-01-05 19:03:08 +00:00
|
|
|
assert_have_no_tag("div", :class => "nope")
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body does contain the tag" do
|
2009-01-05 19:03:08 +00:00
|
|
|
lambda {assert_have_no_tag("div")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should throw an exception when the body contains the tag with additional selectors" do
|
2009-01-05 19:03:08 +00:00
|
|
|
lambda {assert_have_no_tag("div", :class => "inner")}.should raise_error(Test::Unit::AssertionFailedError)
|
2009-01-04 23:42:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
2008-11-09 02:34:46 +00:00
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
describe Webrat::Matchers::HasContent do
|
|
|
|
include Webrat::Matchers
|
2008-11-09 02:34:46 +00:00
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
before(:each) do
|
2008-11-07 05:35:50 +00:00
|
|
|
@body = <<-EOF
|
2008-11-06 19:05:52 +00:00
|
|
|
<div id='main'>
|
|
|
|
<div class='inner'>hello, world!</div>
|
|
|
|
</div>
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#matches?" do
|
2008-11-09 02:34:46 +00:00
|
|
|
it "should call element#contains? when the argument is a string" do
|
2008-11-07 05:35:50 +00:00
|
|
|
@body.should contain("hello, world!")
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
|
2008-11-09 02:34:46 +00:00
|
|
|
it "should call element#matches? when the argument is a regular expression" do
|
2008-11-07 05:35:50 +00:00
|
|
|
@body.should contain(/hello, world/)
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
end
|
2008-12-27 22:44:54 +00:00
|
|
|
|
|
|
|
describe "asserts for contains," do
|
2009-01-14 04:24:55 +00:00
|
|
|
include Test::Unit::Assertions
|
2008-12-31 23:01:46 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2008-12-27 22:44:54 +00:00
|
|
|
describe "assert_contain" do
|
|
|
|
it "should pass when containing the text" do
|
|
|
|
assert_contain("hello, world")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should pass when containing the regexp" do
|
|
|
|
assert_contain(/hello, world/)
|
|
|
|
end
|
|
|
|
|
2008-12-27 23:48:31 +00:00
|
|
|
it "should throw an exception when the body doesnt contain the text" do
|
2008-12-31 23:01:46 +00:00
|
|
|
lambda {assert_contain("monkeys")}.should raise_error(Test::Unit::AssertionFailedError)
|
2008-12-27 22:44:54 +00:00
|
|
|
end
|
|
|
|
|
2008-12-27 23:48:31 +00:00
|
|
|
it "should throw an exception when the body doesnt contain the regexp" do
|
2008-12-31 23:01:46 +00:00
|
|
|
lambda {assert_contain(/monkeys/)}.should raise_error(Test::Unit::AssertionFailedError)
|
2008-12-27 22:44:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "assert_not_contain" do
|
|
|
|
it "should pass when not containing the text" do
|
2008-12-31 23:01:46 +00:00
|
|
|
assert_not_contain("monkeys")
|
2008-12-27 22:44:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should pass when not containing the regexp" do
|
|
|
|
assert_not_contain(/monkeys/)
|
|
|
|
end
|
|
|
|
|
2008-12-27 23:48:31 +00:00
|
|
|
it "should throw an exception when the body does contain the text" do
|
2008-12-31 23:01:46 +00:00
|
|
|
lambda {assert_not_contain("hello, world")}.should raise_error(Test::Unit::AssertionFailedError)
|
2008-12-27 22:44:54 +00:00
|
|
|
end
|
|
|
|
|
2008-12-27 23:48:31 +00:00
|
|
|
it "should throw an exception when the body does contain the regexp" do
|
2008-12-31 23:01:46 +00:00
|
|
|
lambda {assert_not_contain(/hello, world/)}.should raise_error(Test::Unit::AssertionFailedError)
|
2008-12-27 22:44:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-11-09 02:34:46 +00:00
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
describe "#failure_message" do
|
2008-12-27 22:44:54 +00:00
|
|
|
|
2008-11-06 19:05:52 +00:00
|
|
|
it "should include the content string" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new("hello, world!")
|
2008-11-09 02:34:46 +00:00
|
|
|
hc.matches?(@body)
|
2008-11-06 19:05:52 +00:00
|
|
|
|
|
|
|
hc.failure_message.should include("\"hello, world!\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the content regular expresson" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
|
2008-11-09 02:34:46 +00:00
|
|
|
hc.matches?(@body)
|
2008-11-06 19:05:52 +00:00
|
|
|
|
|
|
|
hc.failure_message.should include("/hello,\\sworld!/")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the element's inner content" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
|
2008-11-07 05:35:50 +00:00
|
|
|
hc.matches?(@body)
|
2008-11-06 19:05:52 +00:00
|
|
|
|
2008-11-07 05:35:50 +00:00
|
|
|
hc.failure_message.should include("hello, world!")
|
2008-11-06 19:05:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|