RSpec 2 View test to validate accessible markup?

和自甴很熟 提交于 2020-01-15 08:45:47

问题


I want to make some basic accessibility tests in RSpec (obviously, to be further validated by other tools and users later; this is to catch the low-hanging fruit like finding images w/o alt tags and such)

Most of the examples have just checking content is present is similar; what I want to do is get a list of tags, and then make assertions that "all" the tags found meet certain criteria (e.g. all images have to have either an alt or a longdesc; each form input needs either a label or title, etc).

Can RSpec do this, or if not, is there a tool that can?

Thanks.


回答1:


You can use webrat to test for XPath selectors on your view specs:

describe 'my/view.html.erb' do
  it 'should not have images without alt or longdesc attributes' do
    render
    rendered.should_not have_xpath('//img[not(@alt) and not(@longdesc)]')
  end
end

Capybara supports XPath selectors, too.



来源:https://stackoverflow.com/questions/7199597/rspec-2-view-test-to-validate-accessible-markup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!