Rspec 水豚:应该有html内容

Rspec 水豚:应该有html内容,rspec,capybara,Rspec,Capybara,如何使用capybara检查页面上的html内容 page.should have_text('this is <b>bold</b> "text"') page.应该有_文本('这是粗体的“文本”) 输出: Failure/Error: page.should have_text('this is <b>bold</b> "text"') expected there to be this is <b>bold</b> \"text\"

如何使用capybara检查页面上的html内容

page.should have_text('this is <b>bold</b> "text"')
page.应该有_文本('这是粗体的“文本”)
输出:

Failure/Error: page.should have_text('this is <b>bold</b> "text"') expected there to be this is <b>bold</b> \"text\" in "....this is bold \"text\"....." 失败/错误:第页应具有_文本('这是粗体的“文本”) 预计在“…这是粗体的”文本“…中会有粗体的”文本“…”
您的代码不工作,因为Capybara的
have_text?
(因此
have_text
)方法检查文本,而不是调用它的元素的html源

如果您使用支持Javascript评估的驱动程序(例如Selenium、Poltergeist或Capybara Webkit),您可以使用Javascript获取元素的内部HTML:

html = page.evaluate_script("document.getElementById('answer-15988092').innerHTML")
html.should include('this is <b>bold</b> "text"')

通常我更喜欢javascript评估,因为它更具限制性。

我尝试了许多有效元素,包括
id
div\id
body
。在每种情况下,我都会遇到这样的拒绝
未知错误:无法读取null的innerHTML属性
page.html.should include('this is <b>bold</b> "text"')