Capybara断言元素是空的

Mic*_*rey 6 testing dom capybara

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

<table id="foo">
  <tr>
    <td>Cell One</td>
    <td></td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

但这两个回归true:

find("#foo td:nth-child(1)").should have_content('')
find("#foo td:nth-child(2)").should have_content('')
Run Code Online (Sandbox Code Playgroud)

所以我用过这个:

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

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

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

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

Jus*_* Ko 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==''
Run Code Online (Sandbox Code Playgroud)