You are here: Browse Railsplugins Hpricot Forms
I love to use HtmlUnit to do web-based navigation tests, back in Java land. So I’m attempting to re-create a similar testing experience here.
Installation./script/plugin install http://choonkeat.svnrepository.com/svn/rails-plugins/hpricot_forms
ExamplesIn a hypothetical application login application, we write the functional test like this:
def test_login_logout page = get :new # first, let’s get to the login page form = page.forms.first # next, get a reference to the form end
form["options[]"] = ["secure", "remember"]
# e.g. options[] fields are checkboxes
form["user[login]"] = "Joe"
form["user[password]"] = "topsecret"
# these are single-value inputs
page = form.submit(self) # after setting the form values, submit it
# 'page' now references the page after login
assert_response :success
page = page.links("@id='signout'").first.click(self)
# locate the sign-out link by its html ID
# attribute and click it
assert_response :success
Notice we didn’t need to handle things like session and where the form submits to explicitly. It simply goes to where its supposed to – as rendered. And yes, as you do submit() and click() you might actually cross different controllers. HpricotForms handles that for you.
The main thing I like about this API is, the field names in the form are real. When you modify your action template (.rhtml files) but forgot to change your test and controllers – traditionally, your tests will still pass because you were passing in parameters explicitly, e.g.
post :login, {:user => {:login => "Joe", :password => "topsecret"}}
even though your HTML form fields might have been changed to “account[login]” and “account[passwd]”.
But HpricotForms will raise an exception when you try to set values into fields that does not exist in the rendered form.
NotesI don't really like doing 'instance_variable_get' but that's how I can get it to work now. Even worse, I don't like the explicit 'self' argument required to do submit() and click(). Any suggestion will be appreciated.
TestsNot implemented
RakeNot implemented
Thanks toHtmlUnit’s API [http://htmlunit.sourceforge.net/gettingStarted.html] _why’s Hpricot library [http://redhanded.hobix.com/inspect/hpricot01.html]
LicenseHpricotForms is released under the MIT license.
AuthorChew Choon Keat <choonkeat gmail at> http://blog.yanime.org/
19 July 2006
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly