Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/Capybara: How to click a link in a table row that also contains unique text

Tags:

rspec

capybara

I have an app that does not assign unique IDs to table cells. Given unique text in column 1, a manage link in column 2, and a delete link in column 3, how do I tell capybara to click the manage link in the same row that contains the text 'Foo'?

Foo   manage  delete
Bar   manage  delete

I see how to use find('tr', text: "Foo") when used with a .should clause. And I see how to click a link that contains the text 'manage'. But I do not see how to combine them tofind a row, and within that row, click a link.

like image 890
jpw Avatar asked Oct 12 '12 03:10

jpw


1 Answers

I've tested this and it should work:

find(:xpath, "//tr[td[contains(.,'Foo')]]/td/a", :text => 'manage').click

What this says is, find the row with a column which contains the text 'Foo', and click on the link in a column in the same row with a text 'manage'.

If you don't care where 'Foo' appears (i.e. could be anywhere in the row), then you can do this:

find(:xpath, "//tr[contains(.,'Foo')]/td/a", :text => 'manage').click
like image 64
Chris Salzberg Avatar answered Sep 22 '22 21:09

Chris Salzberg