Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created December 25, 2008 22:21
Show Gist options
  • Save vangberg/39960 to your computer and use it in GitHub Desktop.
Save vangberg/39960 to your computer and use it in GitHub Desktop.
# Easy view testing for Rails with Hpricot.
#
# Prefix 'response' with @ for Sinatra.
class Test::Unit::TestCase
# elements('h1') returns a Hpricot::Elements object with all h1-tags.
def elements(selector)
Hpricot(response.body).search(selector)
end
# element('h1') returns Hpricot::Elem with first h1-tag, or nil if none exist.
def element(selector)
Hpricot(response.body).at(selector)
end
# tags('h1') returns the inner HTML of all matched elements mathed.
def tags(selector)
e = elements(selector)
e.map {|x| x.inner_html}
end
# tag('h1') returns the inner HTML of the first mached element, or nil if none matched.
def tag(selector)
e = element(selector)
e && e.inner_html
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment