Search code examples
rubycapybara

Searching for records with capybara


Our Thomas Walpole helped me a lot in this logic below. I have a problem, when the record is on the first page the button that moves to the next page in the list is disabled. How do I so if the record is on the first page it does not enter the loop that scans the screen looking for this record?

def cad_diametro_material_ativo
    input_codigo.send_keys('aut_Server_AL')
    input_ordem.send_keys('1')
    @gera_material_ativo =  'AUT_Server_ALUMINIO_ATIVO'+ rand(1..99).to_s
    input_descricao.send_keys(@gera_material_ativo)
    btn_inserir.click
    until page.has_css?("td", text: @gera_material_ativo, wait: 3) do
    btn_move_registros.click
    end
    find("td", text: @gera_material_ativo).click
    btn_cancelar.click
    end

    hml code

    <div id="AFRAME_14bb" onmousedown="event.preventMoving=true;" style="display: inline-block; box-sizing: border-box; min-width: 100%;" class="tv">
       <a name="tvTDiametroMat"></a>
       <div class="tvCaption">
          <div style="display:inline-block;width:100%"><img src="r/std/static/pixel.gif" class="icon Enabled icon_grid" draggable="false" onclick="sendEvent(0,event,this,true,0,'','14bb','ControlMenu#','','','','','');">&nbsp;Diâmetro do Material</div>
       </div>
       <div id="TV-tvTDiametroMat">
          <div style="" class="tvContent">
             <div style="display:table;width:100%; background:inherit;">
                <div style="display:table-cell;vertical-align:top; background:inherit;" class="tvNavigation">
                   <div class="tvNavigation">
                      <div>1</div>
                      <div><img src="r/std/static/pixel.gif" class="icon icon_ro-upup" draggable="false"></div>
                      <div><img src="r/std/static/pixel.gif" class="icon icon_ro-up" draggable="false"></div>
                      <div><img src="r/std/static/pixel.gif" class="icon Enabled icon_down" draggable="false" onclick="loadPage('tvTDiametroMat','1','11',2);"></div>
                      <div><img src="r/std/static/pixel.gif" class="icon Enabled icon_downdown" draggable="false" onclick="loadPage('tvTDiametroMat','10','11',2);"></div>
                   </div>

enter image description here


Solution

  • until page.has_css?("td", text: @gera_material_ativo, wait: 3) do
      btn_move_registros.click
    end
    

    will only click the button to go to the next page if the first page doesn't have the desired td element within the specified wait period. If it is clicking to go the next page even though you believe the expected td is on the first page then it's likely your wait setting isn't long enough for the first page to render. Try increasing the wait value