rspec-html-matchers icon indicating copy to clipboard operation
rspec-html-matchers copied to clipboard

Matching text content with other attributes

Open asbjornu opened this issue 5 years ago • 1 comments

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 avatar Sep 17 '20 12:09 asbjornu

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.

asbjornu avatar Sep 17 '20 12:09 asbjornu