The stale content is below for a limited time for your convenience while DNS caches get expired.
Automated Testing of HTML5 Microdata in Rails
Published: 2013-03-22 22:50:00 -0400
While HTML5 Microdata has the advantage of using visible markup, it can still be invisible enough that when your app changes your Microdata goes out of sync. Testing your Microdata is important for ensuring your Microdata parses correctly and you’re communicating to the search engines what you think you are. I’ll show you a simple example of test-first addition of Microdata in a Rails project.
Here’s what the HTML looks like. The indented formatting is just to make it more readable.
We have a person’s name, a job title, and the organization the person is affiliated with.
Test Preparation
We’ll start with writing some tests for what we want the resulting data to look like. I’ll assume that you are already using Capybara for your integration tests. To get set up all we have to do is add the microdata gem to our Gemfile for the test environment and bundle install.
Tests
We’ll write these tests as integration tests using test-unit. To generate a test run:
Open up test/integration/microdata_test.rb and add your tests for how you’d expect the Microdata to parse:
Run the tests. OK, these tests fail as they should.
HTML with Microdata
Here’s how we’ll update the HTML for the tests to pass.
And it doesn’t look any different, but here’s it is:
Thank you to Lawrence Woodman for making the mida gem available under a permissive license which allowed me to extract out just the bits I was interested in for the microdata gem. If you need vocabulary awareness, then mida might be better for you.