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?
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==''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With