Dewald Schoonderwoerd Dewald Schoonderwoerd - 16 days ago 6
Ruby Question

How to stop the infinite loop with a break

def find_a_specific_security_channel_inside_a_table
expect(page).to have_css('#datatableGeneric')

loop do
td = page.all('td', text: 'NAME')
Timeout.timeout(5) do
if td.empty?
click_link ('Next')
sleep 1
else
find('#datatableGeneric', :text => 'NAME').click
break
end
end
end
return true
end


Should loop through a table's content until it finds (
'NAME'
) string, and finally break out of the loop. But it does not break out of the loop.

Answer

You are only breaking from the timeout block and not from the main loop.

loop do
  td = page.all('td', text: 'NAME')
  break_loop = false

  Timeout.timeout(5) do
    if td.empty? 
      click_link ('Next')
      sleep 1
    else
      find('#datatableGeneric', :text => 'NAME').click
      break_loop = true
    end
  end

  break if break_loop
end
Comments