1

In Rspec Capybara test I want to check that no H4, H5, ..., H9 selector exists.

I would expect the following solution:

visit "/"
expect(page).not_to have_css(/(h|H)\[4-9]d/)

But it fails due to Nokogiri parsing

I also noticed that:

has_css?("h1")

# is not equal to

has_css?("H1")
dpaluy
  • 3,537
  • 1
  • 28
  • 42
  • why not just "h1,h2,h3,h4,h5,h6,h7,h8,h9". I'm not aware that nokogiri takes regex selectors at all. If you are too lazy to type them out you could do `Array(1..9).map { |n| 'h' + n.to_s }.join(',')` – max Apr 21 '15 at 10:03
  • @papirtiger This is what I do, but there should be a better way to test it. I think that has_css? should be case insensitive. – dpaluy Apr 21 '15 at 10:07
  • How is that really an issue unless you are testing HTML from 1995? – max Apr 21 '15 at 10:11
  • I should verify that specific page doesn't have H* tags (SEO requirement) – dpaluy Apr 21 '15 at 11:02

1 Answers1

1

Some alternatives:

1. Just create a slightly ugly expectation and get it done:

visit "/"
expect(page).not_to have_css("h1,H1,h2,H2,h3,H3,h4,H4,h5,H5,h6,H6,h7,H7,h8,H8,h9,H9")

I used the following snippet to generate that ugly selector:

(1..9).map { |n| "h#{n.to_s},H#{n.to_s}" }.join(',')

2. Create a case insensitive XPath selector:

How can I create a nokogiri case insensitive Xpath selector?

3. Use Nokogumbo

Nokogumbo is a HTML5 parser which creates lowercase element names.

Community
  • 1
  • 1
max
  • 96,212
  • 14
  • 104
  • 165