Here's a technique I've been using, and I figure others might find it
useful.
I've always wanted an automated way to validate my project's (X)HTML
markup. In July, the W3C Validator made it easy, by allowing fragments
to be POSTed to their service. Now, with a simple custom assertion,
your functional tests can ensure your markup is valid, and that it
stays valid. Just add this to your test/test_helper.rb:
def assert_valid_markup(markup=@response.body)
require 'net/http'
response = Net::HTTP.start('validator.w3.org') do |w3c|
query = 'fragment=' + CGI.escape(markup) +
'&output=xml'
w3c.post2('/check', query)
end
assert_equal 'Valid', response['x-w3c-validator-status']
end
And then (given an action called 'home') call the assertion from your
functional tests like so:
def test_home
get :home
assert_valid_markup
end
And presto, your markup will be validated every time you run your
tests, and if something happens to invalidate your output, you'll know
it right away. By default, assert_valid_markup() will validate the
contents of @response.body (which is set by the get line); you can
also pass it an argument to check something else. If you want more
detail on the validation errors, modify the assertion to look at
response.body — it's an XML file with helpful information about each
error.
I'm sure there's room for improvement, so I'd appreciate any
suggestions. I've found it's useful in every project I'm working on;
if anybody else agrees, I'll submit it as a patch for consideration.
http://scottraymond.net/articles/2005/09/20/rails-xhtml-validation
:sco
redgreenblu.com, blinksale.com
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
This is a great idea, I''m going to give it a try. Does anyone know if there is a way to accomplish this wthout having to send an HTTP request to w3.org? It would be nice to write functional test that will pass even when you''re not connected to the internet. Is there a ruby xhtml validation library? On 9/20/05, Scott Raymond <scottraymond@gmail.com> wrote:> > Here''s a technique I''ve been using, and I figure others might find it > useful. > > I''ve always wanted an automated way to validate my project''s (X)HTML > markup. In July, the W3C Validator made it easy, by allowing fragments > to be POSTed to their service. Now, with a simple custom assertion, > your functional tests can ensure your markup is valid, and that it > stays valid. Just add this to your test/test_helper.rb: > > def assert_valid_markup(markup=@response.body) > require ''net/http'' > response = Net::HTTP.start(''validator.w3.org'') do |w3c| > query = ''fragment='' + CGI.escape(markup) + ''&output=xml'' > w3c.post2(''/check'', query) > end > assert_equal ''Valid'', response[''x-w3c-validator-status''] > end > > And then (given an action called ''home'') call the assertion from your > functional tests like so: > > def test_home > get :home > assert_valid_markup > end > > And presto, your markup will be validated every time you run your > tests, and if something happens to invalidate your output, you''ll know > it right away. By default, assert_valid_markup() will validate the > contents of @response.body (which is set by the get line); you can > also pass it an argument to check something else. If you want more > detail on the validation errors, modify the assertion to look at > response.body ? it''s an XML file with helpful information about each > error. > > I''m sure there''s room for improvement, so I''d appreciate any > suggestions. I''ve found it''s useful in every project I''m working on; > if anybody else agrees, I''ll submit it as a patch for consideration. > > http://scottraymond.net/articles/2005/09/20/rails-xhtml-validation > > :sco > redgreenblu.com, blinksale.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060130/88bcaf4a/attachment.html
Paul Barry wrote:> This is a great idea, I''m going to give it a try. Does anyone know if > there is a way to accomplish this wthout having to send an HTTP request > to w3.org <http://w3.org>? It would be nice to write functional test > that will pass even when you''re not connected to the internet. Is there > a ruby xhtml validation library?I don''t know about a Ruby validation library, but you could always pipe it through Tidy... -- Alex
Paul Barry wrote:> This is a great idea, I''m going to give it a try. Does anyone know if > there is a way to accomplish this wthout having to send an HTTP request > to w3.org <http://w3.org>? It would be nice to write functional test > that will pass even when you''re not connected to the internet. Is there > a ruby xhtml validation library?The assert_valid_markup plugin now catches SocketError exceptions, so that the assertion will pass if there''s temporarily no connection. Plus, it caches responses from the W3C validator, so it only makes a connection if the markup has changed. It also has class-level methods, so you can do things like: class FooControllerTest < Test::Unit::TestCase assert_valid_markup :home_page, :another_page assert_valid_markup :some_private_page, :but_first => :login end The plugin lives here: http://redgreenblu.com/svn/projects/assert_valid_markup/ As such, this should install it for you: script/plugin install http://redgreenblu.com/svn/projects/assert_valid_markup Scott Raymond