Hpricot is: Enjoyable website testing
George Malamidis, April 4th, 2007Hpricot is certainly much more than that. In fact, Hpricot is A Fast, Enjoyable HTML Parser for Ruby. Even though the possibilities are limited by one’s imagination, I find testing websites with Hpricot particularly breezy:
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'test/unit'
class GoogleTest < Test::Unit::TestCase
def setup
@base = 'http://google.com'
end
def test_language_tools_title
page = click_link('Language Tools')
assert_equal('Language Tools', page.at('title').inner_html)
end
private
def click_link(link)
doc = Hpricot(open(@base))
link = (doc/'a').detect { |e| e.inner_html == link }
Hpricot(open("#{@base}#{link[:href]}"))
end
end
Add the flexibility of the Ruby Net::HTTP API to the mix - for submitting forms, authentication, accessing resources behind proxies, etc, stir and serve chilled.
