Home > Net >  How to validate if the table is empty with capybara?
How to validate if the table is empty with capybara?

Time:04-14

I have tried all these commands but none works. I even used some tips from StackOverflow but I wasn't happy yet.. I have tried all these commands but none works. I even used some tips from StackOverflow but I wasn't happy yet.. I have tried all these commands but none works. I even used some tips from StackOverflow but I wasn't happy yet..

expect(find('.tvGrid').has_no_content?).to be true
expect(find('.tvGrid')).to have_no_content
expect(find('.tvGrid > tbody', visible: false)).not_to have_css('td')
expect(find('.tvGrid > tbody')).to have_no_content
<table width="100%" >
  <tbody>
    <tr>
      <th colspan="1" >Id</th>
      <th colspan="1" >Code</th>
      <th colspan="1" >Description</th>
      <th colspan="1" >Operational Center</th>
      <th colspan="1" >Status</th>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr >
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
     </tr>
  </tbody>
</table> 

CodePudding user response:

The point is that you can check that there is no text inside td nodes. There are many ways to do it

As example

expect(all('.tvGrid td').any? { |td| td.text.present? }).to be false

or

expect(all('.tvGrid td').select { |td| td.text.present? }).to be_empty

or

expect(all('.tvGrid td').all? { |td| td.text.blank? }).to be true

.blank? and .present? are ActiveSupport methods. In vanilla Ruby you can use .strip.empty? instead of these methods

CodePudding user response:

The key here is tell Capybara to expect no td elements with contents. You can use the fact the text option can be a Regexp here to check for that

expect(page).not_to have_css('.tvGrid td', text: /. /)

This will make Capybara ensure there are no elements matching the .tvGrid td selector with 1 or more characters of text content, and will retry that for up to Capybara.default_max_wait_time seconds to account for dynamic pages. You can adjust the Regexp as necessary to allow any acceptable contents.

  • Related