Finding a specific row in a table using capybara
A nice clean approach would be to add a "data-user-id" attribute to each tr element and then find the row you want with tr = find('tr[data-user-id="22"]')
, but if that's not an option there are a number of ways to do this. Either of
td = page.find(:css, 'td.id', text: /^22$/) # find the id td with text of exactly 22
tr = td.find(:xpath, './parent::tr') # get the parent tr of the td
expect(tr).to have_css('td.name', text: 'John Smith')
or finding the row using just an xpath like
tr = page.find(:xpath, ".//tr[./td[@class='id'][text()='22']]")
expect(tr).to have_css('td.name', text: 'John Smith')
should do what you want. If you want to stick with the looping approach (not recommended because it will be slow - and the way your loop is structured it could just not verify anything) it would be
page.all('tr').each do |tr|
next unless tr.has_css?('td.id', text: /^22$/)
expect(tr).to have_css('td.name', text: "John Smith")
#other expects here...
end