用 RSpec 和 Capybara 匹配具有 HTML 标签的文本

问题描述 投票:0回答:1

我正在使用 RSpec 和 Capybara 来期待包含 HTML 标签的文本,如下所示:

expect(page).to have_css('#dom_id', :text => "Text with <b>HTML tags</b>", :visible => true, :count => 1)

当我运行自动化测试时,我得到:

Failure/Error: expect(page).to have_css('#dom_id', :text => "Text with <b>HTML tags</b>", :visible => true, :count => 1)
  expected to find visible css "#dom_id" with text "Text with <b>HTML tags</b>" 1 time but there were no matches. 
  Also found "Text with HTML tags", which matched the selector but not all filters. 

通过模拟人类测试(即手动浏览页面以显示文本),我正确地得到了 HTML 解析的

Text with <b>HTML tags</b>
输出:“带有HTML 标签的文本”(即带有粗体“HTML 标签的文本” ").

我怎样才能使测试通过考虑/匹配文本中的 HTML 标签?

html testing rspec capybara rspec-rails
1个回答
0
投票

<b>
不是文本,它是页面中的一个元素,因此您无法使用 :text 选项验证它。如果对大胆的东西进行测试真的很重要,你可以像

expect(page).to have_css('#dom_id b', exact_text: 'HTML tags')

如果你真的坚持做你要求的事情,你可能会在过滤器块中用 JS 来做,比如

expect(page).to (have_css('#dom_id') do |el|
  el.evaluate_script('this.innerHtml') == 'Text with <b>HTML tags</b>'
end)
      

但我不会推荐它作为一个正常的用例。

© www.soinside.com 2019 - 2024. All rights reserved.