Hpricot Forms plugin

Plugin details

An attempt to create similar functionality to HtmlUnit to do web-based navigation tests.

Websitehttp://blog.yanime.org/articles/2006/07/19/hpricotforms Repositoryhttp://choonkeat.svnrepository.com/svn/rails-plugins/hpricot_forms/ Author Chew Choon Keat Tags form, HTML LicenseUnknown

Documentation

Install the plugin:
ruby script/plugin install http://choonkeat.svnrepository.com/svn/rails-plugins/hpricot_forms/

Anyways, though the whole testing harness of Rails is a godsend, I started to miss some of those HtmlUnit goodies - pulling out a Form object from a visited page, setting the fields values and submitting it.. and getting back a Page. So, this is what I've come up with for use in functional tests:

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
   # form.field_names => ["user[login]", "user[password]"]
                              # form.field_names returns you the input fields
                              # available in the form
 
   form["user[login]"]    = "Joe"
   form["user[password]"] = "topsecret"
   page = form.submit(self)   # after setting the form values, submit it
                              # 'page' now references the page after login
   assert_response :success
   # page.links               # returns you an array of links found on the page
 
   page = page.links("@id='signout'").click(self)
                              # locate the sign-out link by its html id
                              # and click it
   assert_response :success
 end 


Can't say its a replica of HtmlUnit... but it does meet my needs for now. Notice we didn't need to explicitly handle things like session or where the form submits to. It simply goes to where its supposed to go - as rendered by the view. 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 that the field names in the form are real. If you're careless like me, and modify your action templates (.rhtml files) but forgot to change both your controller and test, traditionally your test 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 were 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.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 10 months ago