view raw
GlyphGryph GlyphGryph - 11 months ago 54
Ruby Question

Finding if a content exists in a given column in a table, with Capybara or Xpath?

Given a table, a specific piece of content should appear in the same column as a specific header.

I can not test the column position numerically, or at least I can't hardcode it that way, since the number of columns can change based on various other conditions and I don't want to make my test that fragile.


Name || Phone Number || Address
... || ... || ...
Joe || 555-787-7878 || 42 Nowhere Lane, Mulberry, California
... || ... ||

With the code looking like so:

<th>Phone Number</th>

<td>42 Nowhere Lane1, Mulberry1, California</td>
<td>50 Nowhere Lane, Mulberry, California</td>

<td>43 Nowhere Lane2, Mulberry2, California</td>

I want to insure the correct address (42 Nowhere...) appears in the column with the header "Address".

How can I do this?

The solution might be as simple as a decent xpath query, to be honest, perhaps I don't even need anything particularly "Capybara" related!

I came across similar one, but here I need to check whether 'Jerry' registered or not. Please help me how can i automate using ruby/capybara

Thanks in Advance


I think @Snekse is very close. Here are some expressions that have been tested. The following returns the table cell corresponding to the th whose value is Address:


This can be used to get the value of the cell:

42 Nowhere Lane, Mulberry, California

...which you could then use to perform the final test:

                               [.='42 Nowhere Lane, Mulberry, California']

This will return the cell itself, if the addresses match. It will return an empty node-set, which will evaluate to false in a boolean context, if no such node exists.

You probably also need to specify the row you're testing, which can be done with a slight modification to the start of the expression:


...where $n is the index of the row to select.