Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matching text content with other attributes #74

Open
asbjornu opened this issue Sep 17, 2020 · 1 comment
Open

Matching text content with other attributes #74

asbjornu opened this issue Sep 17, 2020 · 1 comment

Comments

@asbjornu
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant