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

Failing asserting text inside tag #64

Closed wbotelhos closed 4 years ago

wbotelhos commented 7 years ago

Thank you for your great gem, it is so useful e save me a lot of time. (:

I found some strange behavior, where I cannot match text inside the tag, so I need to match only the tags inside, but the text is important to me, check it out:

expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message: '

  with_tag 'b' do
    with_text 'Hello'
  end
end
Failure/Error: with_text 'E-mail não confirmado: '

"E-mail não confirmado: " expected within "span" in following template:
<span>E-mail não confirmado: <b>unconfirmed@domain.com</b></span>
expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message: <b>Hello</b>'
end
Failure/Error: with_text 'Message: '

"Message: " expected within "div" in following template:
<div>Message: <b>Hello</b>
</div>
expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message: <b>Hello</b>'.html_safe
end
Failure/Error: with_text 'Message: <b>Hello</b>'

"Message: <b>Hello</b>" expected within "div" in following template:
<div>Message: <b>Hello</b>
</div>

One solution is wrap the text inside a tag. Any idea here?

Thanks.

roooodcastro commented 6 years ago

I'm having the same issue. Seems like it can't match text if there's extra HTML alongside the text inside the tag you're matching.

SirRawlins commented 6 years ago

This is causing me issues today, I'm having the same issue. As @roooodcastro points out, it seems to be that text match fails if there is HTML in the string.

jasonadkison commented 5 years ago

Same issue here.

Using with_text('stat-value'):

"stat-value" expected within "span.icon-name" in following template:
<span class="icon-name"><i class="icon ion-icon-name"></i> stat-value</span>

However using with_text(/stat-value/) works as expected.

randoum commented 4 years ago

Not a bug. There are 2 different concepts to understand here:

Examples :

expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message: Hello' 
end
# => Pass, because the B tag is stripped out. 
#  Remember we are looking at the DIV's inner_text, meaning that all HTML is stripped away
expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text /Message:/
end
# => Pass, because we match against a regexp, so we are only searching for a portion of the text contained in the DIV
expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text /Message:$/
end
# => Fail, because the DIV's inner_text do not end with "Message:"
expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message:'
end
# => Fail, because we match against a string, so we match for the whole content of DIV's inner_text 
wbotelhos commented 4 years ago

Hi @randoum ,

Who can we use nested checks like:

expect('<div>Message: <b>Hello</b></div>').to have_tag('div') do
  with_text 'Message: '

  with_tag 'b' do
    with_text 'Hello'
  end
end

Or I cannot have nested with_tag and should always use a full stripped innerText match?

randoum commented 4 years ago

You can have nested with_tag, as for the full or partial match it depends on your needs.