Skip to content

Instantly share code, notes, and snippets.

@quirkey
Created February 25, 2009 16:01
Show Gist options
  • Select an option

  • Save quirkey/70246 to your computer and use it in GitHub Desktop.

Select an option

Save quirkey/70246 to your computer and use it in GitHub Desktop.
# HTML matcher for bacon
#
# it 'should display document' do
# body.should have_element('#document')
# end
#
# With content matching:
#
# it 'should display loaded document' do
# body.should have_element('#document .title', /My Document/)
# end
require 'nokogiri'
def have_element(search, content = nil)
lambda do |obj|
doc = Nokogiri.parse(obj.to_s)
node_set = doc.search(search)
if node_set.empty?
false
else
collected_content = node_set.collect {|t| t.content }.join(' ')
case content
when Regexp
collected_content =~ content
when String
collected_content.include?(content)
when nil
true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment