Skip to content

jeremyevans/capybara-validate_html5

Repository files navigation

capybara-validate_html5 validates the HTML5 for each page parsed, and fails if there are any HTML5 parse errors on the page. This makes it easy to automatically test for HTML5 validity when running your normal capybara test suite.

This only works for the rack-test driver.

gem install capybara-validate_html5

Source code is available on GitHub at github.com/jeremyevans/capybara-validate_html5

require 'capybara/validate_html5'

describe 'capybara-validate_html5' do
  include Rack::Test::Methods
  include Capybara::DSL

  def app
    MyRackApp
  end

  it "should allow restoring of state" do
    visit '/' # No validation on retrieval
    page.body # No validation on body access

    page.all("a") # Attempt to parse body, validates HTML, raises exception if not valid
    page.all("a") # No validation, as same body already parsed

    visit '/page1'
    click_link 'Go Somewhere' # Attempt to parse body, validates HTML, raises exception if not valid

    visit '/page2'
    skip_html_validation do
      click_button 'Submit' # No validation, as it was explicitly skipped
    end
  end
end

You can perform your own custom validation of HTML elements, by calling Capybara.custom_html_validation. This will be passed the Nokogiri document and a block, and should call the block with any Nokogiri elements that are considered invalid:

Capybara.custom_html_validation do |doc, &block|
  s = "*[required=true], *[required=false], input[checked=true], input[checked=false]"
  doc.css(s).map(&block)
end

MIT

Jeremy Evans <code@jeremyevans.net>

About

Validate HTML5 for each page accessed when testing with capybara

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages