1

How can I use cucumber to click an edit link in a list of items in a table using xpath?

I've got a selector working that returns the link I need, but it's one of many results.... how can I return only 1 result?

Here is my cucumber step

When /^I click the "([^\"]*)" link for "([^\"]*)"$/ do |link, cell_value|
  within "//*[.//td[contains(.,'#{cell_value}')] and .//a[text()='#{link}']]" do |scope|
    scope.click_link link
  end
end

As you can see this will work, but will just click the first edit link in the table even though it knows about the others that it has matched... so that's no good.

Here is the table and selector: http://pastie.textmate.org/private/9od335pmsj4hi9nbe9lbow

Thank you!

This is the source of that file as well in case the service is down or linking to external code is frowned upon.

## HTML source table

<table>
  <tr>
    <th>
      Name
    </th>
    <th>
      Enabled
    </th>
    <th>
      Does Nicotine Testing
    </th>
    <th colspan='3'>
      Actions
    </th>
  </tr>
  <tr>
    <td nowrap='nowrap'>
      Microsoft
    </td>
    <td>
      Yes
    </td>
    <td>
      No
    </td>
    <td nowrap='nowrap'>
      <a href="/employers/407">Show Portal</a>
    </td>
    <td>
      <a href="/employers/407/edit">Edit</a>
    </td>
    <td>
      <a href="/employers/407" onclick="if (confirm('Are you sure you want to delete the employer \'Microsoft\'? \n\nAll of this employer\'s locations will be deleted which include: \n  \nAll users associations to those locations will also be removed. \n\nThere is NO UNDO. Proceed?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete</a>
    </td>
  </tr>
  <tr>
    <td nowrap='nowrap'>
      IBM
    </td>
    <td>
      Yes
    </td>
    <td>
      No
    </td>
    <td nowrap='nowrap'>
      <a href="/employers/406">Show Portal</a>
    </td>
    <td>
      <a href="/employers/406/edit">Edit</a>
    </td>
    <td>
      <a href="/employers/406" onclick="if (confirm('Are you sure you want to delete the employer \'IBM\'? \n\nAll of this employer\'s locations will be deleted which include: \n &nbsp;&nbsp;&nbsp;1) appleton\n \nAll users associations to those locations will also be removed. \n\nThere is NO UNDO. Proceed?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete</a>
    </td>
  </tr>
</table>

## XPath Selector (not working) trying to target this link: <a href="/employers/406/edit">Edit</a> (the 2nd edit link in the table)

//*[.//text()='IBM' and .//a[text()='Edit']]
jondkinney
  • 85
  • 1
  • 8
  • What is the XML document and what exactly in it needs to be selected? Please, provide sufficient information in your question. – Dimitre Novatchev May 10 '10 at 20:28
  • Dimitre, did you look at the pastie link I have there? It has the full listed in there with the current xpath selector that I'm using. If you look at it you can see that I'm trying to select the "Edit" link in the that has the text "IBM". Let me know if my question is still unclear. Thanks!
    – jondkinney May 10 '10 at 20:34
  • There's a related question/answer over here: http://stackoverflow.com/questions/4248889/cucumber-selecting-an-element-from-a-table-for-deletion-or-addition/7618578#7618578 – mongo296 Oct 01 '11 at 06:32

1 Answers1

1

Your xpath regex ("//*[.//td[contains(.,'#{cell_value}')] and .//a[text()='#{link}']]") is returning more than one "scope" in the form of an array, so you can specify which one to use by specifying the element: scope[0].click_link link # for the first scope[1].click_link link # for the second

hemal
  • 193
  • 2
  • 8