Search code examples
rubycapybara

List only records populated with Capybara


Friends helped me with a solution that validates if there are [active/inactive] records in the list. When I list the records using pp capybara also returns blank lines. How do I disregard empty records?

def validate_active_inactive_records
  expect(page).to have_css("td:nth-child(5)", :text => /^(ACTIVE|INACTIVE)$/) 
 
  # ***listing records***
  page.all('.tvGrid tr > td:nth-child(5)').each do |td|
    puts td.text
  end
end
<table width="100%" class="tvGrid">
  <tbody>
  <tr>
    <th colspan="1" class="tvHeader">Id</th>
    <th colspan="1" class="tvHeader">Code</th>
    <th colspan="1" class="tvHeader">Description</th>
    <th colspan="1" class="tvHeader">Operational Center</th>
    <th colspan="1" class="tvHeader">Status</th>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr class="tvRowEmpty">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
   </tr>
  </tbody>
</table>

Solution

  • Are you asking how to remove the rows with the class tvRowEmpty from your search results? If so, you can use the :not operator in your finder:

    def validate_active_inactive_records
      expect(page).to have_css("td:nth-child(5)", :text => /^(ACTIVE|INACTIVE)$/) 
     
      # ***listing records***
      page.all('.tvGrid tr:not(.tvRowEmpty) > td:nth-child(5)').each do |td|
        puts td.text
      end
    end
    

    If you want to exclude any td that just contains &nbsp; you could use the following finder with a regex that filters tags containing only whitespace characters:

    page.all('.tvGrid tr > td:nth-child(5)', text: /[\s]^*/).each