Search code examples
rubycapybara

How to validate if a value doesn't exist in a table's row count using ruby ​capybara?


There is a column in my table that stores the active and inactive value in the STATUS column. I want to validate if when searching for the ACTIVE status if there are no INACTIVE records. How do I scan this column and use expect to validate that no INACTIVE value has appeared? And if it doesn't appear, I present a message like: puts 'no inactive record found in the list". i tried this: page.all('.tvGrid tr').each do |tr| next unless tr.has_css?('td.Status', text: "ATIVO")

<div style="display:table-cell;">
    <table width="100%" class="tvGrid">
        <tbody>
            <tr>
                <th colspan="1" class="tvHeader">Id</th>
                <th colspan="1" class="tvHeader">Código</th>
                <th colspan="1" class="tvHeader">Descrição</th>
                <th colspan="1" class="tvHeader">Centro Operacional</th>
                <th colspan="1" class="tvHeader">Status</th>
            </tr>
            <tr style="cursor:pointer;" onclick="if(notSelecting()) sendEvent(0,event,this,true,0,'stvSetorAbastecimentos','1cfd','Modify',0,'','','');" class="tvRow tvRowOdd tvRoll">
                <td align="right" class="tvCell" valign="center" nowrap="">21</td>
                <td valign="center" align="left" class="tvCell">02</td>
                <td valign="center" align="left" class="tvCell">BARRETO PIRES</td>
                <td align="left" class="tvCell" valign="center" nowrap="">Águas de Niterói/GSO</td>
                <td valign="center" align="left" class="tvCell">ATIVO</td>
            </tr>
            <tr style="cursor:pointer;" onclick="if(notSelecting()) sendEvent(0,event,this,true,0,'stvSetorAbastecimentos','1cfd','Modify',1,'','','');" class="tvRow tvRowEven tvRoll">
                <td align="right" class="tvCell" valign="center" nowrap="">41</td>
                <td valign="center" align="left" class="tvCell">03</td>
                <td valign="center" align="left" class="tvCell">CAFUBÁ PIRATININGA</td>
                <td align="left" class="tvCell" valign="center" nowrap="">Águas de Niterói/GSO</td>
                <td valign="center" align="left" class="tvCell">ATIVO</td>
            </tr>
            <tr style="cursor:pointer;" onclick="if(notSelecting()) sendEvent(0,event,this,true,0,'stvSetorAbastecimentos','1cfd','Modify',2,'','','');" class="tvRow tvRowOdd tvRoll">
                <td align="right" class="tvCell" valign="center" nowrap="">42</td>
                <td valign="center" align="left" class="tvCell">04</td>
                <td valign="center" align="left" class="tvCell">CAVALÃO</td>
                <td align="left" class="tvCell" valign="center" nowrap="">Águas de Niterói/GSO</td>
                <td valign="center" align="left" class="tvCell">ATIVO</td>
</tr>
</tr>
</tbody>
</table>
</div>

Solution

  • I want to validate if when searching for the ACTIVE status if there are no INACTIVE records.

    Since you want to test the data, not how it is formatted, this is better done as a controller test.

    You can use rails-controller-testing to test what is passed to your views. assigns gives you access to instance variables which have been passed to your views.

    test "should show only active records" do
      # Call the appropriate URL with the appropriate parameters
      get search_url, params: { active: true }
    
      # Check the right template is rendered
      assert_template 'records/index'
    
      # Check the correct data is passed to the view.
      assert_equal Record.active, assigns(:records)
    end