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

<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 == ''

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

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

有没有一种方法可以检查该元素是否为空?

最佳答案

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

# 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==''

关于dom - capybara 断言元素为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18196247/

10-12 12:50
查看更多