Rspec testing for html entities in page content

人走茶凉 提交于 2019-12-04 00:59:42
harlow

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.

html_safe does not remove html tags if thats what you want.

If you will remove html tags before you test you can use sanitize gem.

# in Gemfile
gem 'sanitize'

# in testfile
require 'sanitize'
html = Sanitize.clean(page.body.text)
html.should have_content("Reports &raquo; Aging Reports")

I found a workaround for the time being that uses a regex instead of a string:

find('#breadcrumbs').text.should match(/Reports . Aging Reports/)

This gets the text of the breadcrumbs div, which contains the text I'm looking for, so the scope of the match is limited. This works fine, but I'd still love to know how to match specific html entities.

Sometimes the simplest solution is the right one and things just work...

page.should have_content("Reports » Aging Reports")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!