kucaahbe / rspec-html-matchers

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)
http://rubygems.org/gems/rspec-html-matchers
MIT License
199 stars 90 forks source link

Matching text content with other attributes #74

Open asbjornu opened 4 years ago

asbjornu commented 4 years ago

I want to match a tag by both its attributes and text content, simultaneously. The following spec fails:

html = '<div class="nav-subgroup-heading"><i class="material-icons">arrow_right</i><a href="/intro/">Introduction</a></div>'
expect(html).to have_tag('div', class: 'nav-subgroup-heading') do
  with_tag('i', with: { class: 'material-icons', text: 'arrow_right' })
  with_tag('a', with: { href: '/intro/', text: 'Introduction' })
end

However, if I rewrite the with_tag statements to this:

html = '<div class="nav-subgroup-heading"><i class="material-icons">arrow_right</i><a href="/intro/">Introduction</a></div>'
expect(html).to have_tag('div', class: 'nav-subgroup-heading') do
  with_tag('i', class: 'material-icons', text: 'arrow_right')
  with_tag('a', href: '/intro/', text: 'Introduction')
end

Or this:

html = '<div class="nav-subgroup-heading"><i class="material-icons">arrow_right</i><a href="/intro/">Introduction</a></div>'
expect(html).to have_tag('div', class: 'nav-subgroup-heading') do
  with_tag('i', class: 'material-icons')
  with_tag('i', text: 'arrow_right')
  with_tag('a', href: '/intro/')
  with_tag('a', text: 'Introduction')
end

…it works. I'm not sure the second example actually takes the text: parameter into account at all, and the third one will also match the following, which is not what I want:

<i class="material-icons></i>
<i>arrow_right</i>

Ideas?

asbjornu commented 4 years ago

This also works:

with_tag('i.material-icons', text: 'arrow_right')
with_tag('a[href="/intro/"]', text: 'Introduction')

It would be nice to be able to use the hash syntax instead of having to write CSS selectors, though.