仅列出填充有 Capybara 的记录

List only records populated with Capybara

朋友帮我解决了验证列表中是否有 [active/inactive] 条记录的问题。当我使用 pp capybara 列出记录时,还有 returns 空行。如何忽略空记录?

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>

您是问如何从搜索结果中删除带有 class tvRowEmpty 的行吗?如果是这样,您可以在查找器中使用 :not 运算符:

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

如果您想排除任何仅包含 &nbsp;td,您可以使用以下查找器和一个正则表达式来过滤仅包含空白字符的标签:

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