Reputation: 3628
Looking for the best way to determine if an element is really empty.
<table id="foo">
<tr>
<td>Cell One</td>
<td></td>
</tr>
</table>
But both of these return true
:
find("#foo td:nth-child(1)").should have_content('')
find("#foo td:nth-child(2)").should have_content('')
So I used this:
find("#foo td:nth-child(1)").text.should == 'Cell One'
find("#foo td:nth-child(2)").text.should == ''
Which seems to work, but doesn't check to see if the element may contain other elements. For example it may contain an image, link, or span.
I can check for each one of those individually(image, link, or span), but it seems like there should be better way.
Is there a way to test to see if the element is empty?
Upvotes: 9
Views: 4761
Reputation: 46846
You can do the following to check that the element does not have any text and has no child elements (ie is actually empty):
# 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==''
Upvotes: 8