Rspec testing for html entities in page content

MWean picture MWean · Oct 7, 2011 · Viewed 12.1k times · Source

I'm writing a request spec and would like to test for the presence of the string "Reports » Aging Reports". I get an error (invalid multibyte char) if I put in the character in my matcher expression directly, so I tried this: page.should have_content("Reports » Aging Reports")

This fails the test with the message:

expected there to be content "Reports » Aging Reports" in "\n Reports » Aging Reports\n

I've tried things like .html_safe with no success. Is there a way to test for text containing html entities?

Edit:

Here's the relevant area of the html source:

<a href="/reports">Reports</a> &raquo; <a href="/aging_reports">Aging Reports</a>

Answer

harlow picture harlow · Oct 25, 2012

You can have the test look at the raw source of your page too:

breadcrumb = '<a href="/reports">Reports</a> &raquo; <a href="/aging_reports">Aging Reports</a>'
page.body.should include(breadcrumb)
expect(page.body).to include(breadcrumb) # rspec 2.11+

With that said I'm not sure that's the most elegant solution. Assuming there is a class named .breadcrumb around your links you could just validate the links are present within the div:

within '.breadcrumb' do
  page.should have_css('a', text: 'Reports')
  page.should have_css('a', text: 'Aging Reports')
  expect(page).to have_css('a', text: 'Reports') # rspec 2.11+
  expect(page).to have_css('a', text: 'Aging Reports') # rspec 2.11+
end

This way you are explicitly looking for a href's within the breadcrumb block.