Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara assert element is empty

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?

like image 470
Michael Avatar asked Aug 12 '13 20:08

Michael


1 Answers

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==''
like image 187
Justin Ko Avatar answered Sep 19 '22 15:09

Justin Ko