Easy W3C Validation with Rspec and Webrat
Ever wanted to perform W3C validations on your screens right in your test suite? With the w3c_validators gem (http://code.dunae.ca/w3c_validators/) it is super easy!
First up, you need to install the gem and configure it with your test suite.gem install w3c_validators config.gem 'w3c_validators' Now add a helper method to rspec so we can easily call the validator. Make sure you are including the support directory in your spec_helper.rb. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} Then add a file that contains the helper method. With the below implementation, you can just pass in any path like you would a regular call to "visit" and it will perform the validation for you. spec/support/w3c_validator.rb
include W3CValidators
def validate(url)
visit url
result = MarkupValidator.new.validate_text(response.body)
result.errors.should == []
end
The reason it checks errors against an empty array is so rspec will print out the contents of the errors array when it fails. You can adjust the "should" to match how you want to see the errors in the log. With all that set up, you can now call the validator in your tests
describe 'the home page' do
it 'has valid markup' do
validate root_path
end
end
Being such a simple implementation it is pretty rough around the edges, but it gets the job done!