Whilst working on client projects we often find useful bits of information out. Where possible we blog about that information to pass on our knowledge. Below are some of our most recent entries.
How we added automated html validation to our web development process
As part of our development process we already write lots of integration tests using cucumber. We decided to look into validating our HTML code as part of our integration test suite. It turns out this was incredibly easy to do and we only wish we added this to our tests sooner.
We initially looked at using the markup_validity gem. The major benefit to this gem is that your tests don't have to hit the W3C validator to check for valid code, as the gem compares the markup against schema files directly, thus making validation checks pretty quick. This works fine as long as you aren't using HTML5 for your websites, as the gem doesn't have an appropriate schema file. We could hunt around for an appropriate schema file, but with HTML5 being in a state of flux we would constantly have to update any schema files ourselves. We decided it would be best to use a gem that hit the W3C validator directly.
This gem provides methods to validate X/HTML, CSS and feeds. This was certainly the level of functionality we were going to need. As it hits the W3C validator it also meant we could validate any of our HTML5 work.
To add the gem into cucumber tests, firstly you need to update your env.rb file with a reference to the gem.
We found the best way to add in validation was to update a couple of steps in the web_steps.rb file rather than create specific validation steps in the cucumber features. This keeps the tests as DRY as possible.
include BeValidAsset ....... Given /^(?:|I )am on (.+)$/ do |page_name| visit path_to(page_name) page.body.should be_valid_xhtml if ENV['VALIDATEHTML'] end When /^(?:|I )go to (.+)$/ do |page_name| visit path_to(page_name) page.body.should be_valid_xhtml if ENV['VALIDATEHTML'] end
The ENV['VALIDATEHTML'] makes it easy for us to tell cucumber whether it should run the validation or not.
be_valid_asset comes with some handy configuration settings that help to speed up the validation process. Basically you can added caching to reduce the number of calls to the validators. If a response hasn't changed then the gem knows not to do another lookup. You can add the following code into your env.rb file.
BeValidAsset::Configuration.enable_caching = true BeValidAsset::Configuration.cache_path = File.join(RAILS_ROOT, %w(tmp be_valid_asset_cache))
A quick gotcha