Peter Marklund

Peter Marklund's Home

Wed December 27, 2006

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!


John Philip Green said over 6 years ago:

This looks great! Should replace my crusty old scripts very well. Thank you... I'll let you know if we come up with any patches.


Bill Walton said over 6 years ago:

This is super, Peter! I've been looking for a way to do the w3c validation as part of our integration test. Thanks!

Best regards,


MG said over 6 years ago:

great plugin, is next on my list, since railstidy got some drawbacks and some seriousely stuid nagging, i look forward to try out html_test ;)
if its good ill give it a post too :D


Jarl Friis said over 4 years ago:

The README says
== Installation
ruby script/plugin install

This probably has to be changed now that the source have been moved to github, right?


Peter Marklund said over 4 years ago:

Thanks Jarl. I updated the README.


Craig Walker said over 4 years ago:

Hello Peter. I just installed HTML Test, but I had problems getting it to work because I didn't install the Rails Tidy plugin right off the bat. I see that you mention needing the validator plugins near the bottom of your README. I think it's preferable to list the dependencies near the top of the file (alongside the installation instructions) so that a new user can proceed in the order that the documentation is written; it's more usable this way.

Beyond that: thanks for making a great and useful library.