Capybara断言元素是空的

时间:2013-08-12 20:37:15

标签: dom testing capybara

寻找确定元素是否真的为空的最佳方法。

<table id="foo">
  <tr>
    <td>Cell One</td>
    <td></td>
  </tr>
</table>

但这两个都返回true

find("#foo td:nth-child(1)").should have_content('')
find("#foo td:nth-child(2)").should have_content('')

所以我用过这个:

find("#foo td:nth-child(1)").text.should == 'Cell One'
find("#foo td:nth-child(2)").text.should == ''

这似乎有效,但不检查该元素是否可能包含其他元素。例如,它可能包含图像,链接或跨度。

我可以单独检查每一个(图像,链接或跨度),但似乎应该有更好的方法。

有没有办法测试元素是否为空?

1 个答案:

答案 0 :(得分:6)

您可以执行以下操作来检查元素是否没有任何文本且没有子元素(即实际上是空的):

# Has no child elements
find("#foo td:nth-child(2)").all('*').length.should == 0

# Has no text
find("#foo td:nth-child(2)").text.should==''
相关问题