使用 Capybara 测试斜体

Testing for italics with Capybara

任何人都可以建议在带有水豚的页面上查找斜体的最佳方法。我有一个功能测试,可以在页面中搜索一段特定的文本。此文本为斜体,并且与页面上其他所有内容的颜色也不同。我认为寻找斜体比搜索颜色值更可取,但我不确定该怎么做。

    <div class="text-control">
  <p class="body">Test text is here and then<i>italics</i> are there</p>
</div>

特性测试编写如下:

Then(/^the word should be highlighted in the example sentence$/) do
end

听起来只需要检查句子 p 元素是否包含单词 i 元素。为此,您可以这样做:

# Get the element containing the sentence
sentence_element = find('p.body')

# Check that the sentence includes the italics element with a specific text
expect(sentence_element).to have_css('i', text: 'italics')  

断言文本存在于特定元素中,听起来很合理...您可以使用 Justin 的建议来做到这一点...

测试文本为斜体...这不是您要添加到该测试层或实际上任何层中的测试...