Peter Marklund

Peter Marklund's Home

Wed Dec 27 2006 10:23:56 GMT+0000 (Coordinated Universal Time)

New Rails Plugin for HTML Validation: Html Test

Update on 2009-04-16: The source code has been moved from Google Code to GitHub

Update on 2007-12-12: I have renamed the plugin from "Http Test" to "Html Test" and moved it to a new home at Google Code. Sorry about the inconvenience that this may cause those of you who have the plugin installed and want to update it. The change is essentially a namespace change from Http:: to Html:: so it should be manageable. Thanks.

As I wrote earlier I like to monitor my production sites with HTTP tests that validate their HTML and check for broken links and images. I think that serves as a good baseline check, a smoke test if you like, that the site is not fundamentally broken.

It used to be that my HTTP tests were implemented in Perl using WWW::Mechanize. Now that we have the wonderful Ruby and Ruby on Rails at our fingertips that felt like a real shame. I decided to implement my HTTP tests in Ruby and once I had gotten that far, going the extra mile to package my scripts as a Rails plugin seemed natural. The plugin is called http_test and it's available at Google Code.

So far, what the plugin does is mostly provide support for HTML validation, and to some extent link checking. It provides a command line script that you can use to conveniently HTML validate your site, either in production, staging, or development, as you please. You give the script a URL and it will fetch and validate the page over HTTP. It will also follow links and validate those pages. The script is not a web spider though. It only follows links one step away from the start page.

The other thing you can use the plugin for is to validate pages in your controller and integration tests. The plugin provides the assertion trio assert_tidy, assert_w3c, and assert_xmllint. There is also the catch all assertion assert_validates that can invoke all three validators for you.

What I like to do is setup my application controller to validate all my requests in the test environment. That way you know that all pages that you request in your functional and integration tests validate. The plugin makes this easy. All you have to do is add the following lines to your test_helper.rb:

    ActionController::Base.validate_all = true
    ActionController::Base.validators = [:tidy]

To be able to use Tidy the http_test plugin relies on code in the rails_tidy plugin, so credit to Damien Merenne for providing that. Credit also goes to Scott Baron for the W3C validation, and to Maik Schmidt, author of "Enterprise Integration with Ruby", for the xmllint validation code.

I hope the Html Test plugin will be useful to others in their HTML validation and link checking pursuits. Please let me know what works and what doesn't and how I can improve. Thanks!