我有一个网页,我以适合打印机的格式显示,标题后跟属于该组的项目。 示例:
Headline1
=========
Item1
Item2
Item3
Headline2
=========
Item4
Item5
我想使用 Cucumber + capybara 运行功能测试并验证页面内容是否正确。 我将如何尽可能简单地做到这一点?因为我想确保每个项目都以正确的标题结尾。
我的第一次尝试是使用
page.should have_content"Headline1
=========
Item1
Item2
Item3
Headline2
=========
Item4
Item5"
但这给了我一个奇怪的错误:
expected there to be content "Headline1\n=========\nItem1\n and so on" in "Headline1\n=========\nItem1\n and so on"
也就是说两条线是一模一样的!!!为什么这不起作用? 任何解决方法? 请注意,该页面仅包含基本文本,因此没有 div 项目或我可以在其中检查的内容。
最佳答案
将 page.text
与 include?
或 match
方法一起使用。
http://robots.thoughtbot.com/post/8399836754/multiline-strings-in-capybara http://praktikanten.brueckenschlaeger.org/2011/11/15/working-in-cucumber-with-new-lines-and-have_content
引自第一个链接:
When checking for a multiline string in Capybara, you need to use page.text because page.source does not preserve new lines - so “a\nb” would come out as “a b” in page.source.
关于ruby-on-rails - 使用 capybara has_context 一次检查多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397009/