0
votes

I have a dynnamic table that I would like to use watir to select a edit button. It doesn't have a traditional watir id that I can select (example: browser.img(:title, "iconname")) and there may be multiple edit icons to choose from. In the past I would get the correct element by querying the database. However this does not have a database entry to help me select the correct edit link this time.

In the code below what I am trying to select is from the section where it shows "autogenerated3" I am trying to select either the "onclick" element or the "img src" Both are selectable items that will click the edit icon.

<div id="certificate_table" style="margin-bottom: 1em">
 <table cellspacing="0">
 <tbody>
 <tr>
 <th>Alias</th>
 <th>Common Name</th>
 <th class="centered">Status</th>
 <th class="centered">In Use</th>
 <th class="centered">Issued</th>
 <th class="centered">Expires</th>
 <th class="centered">Days</th>
 <th>Actions</th>
</tr>
<tr class="normal">
<td>autogenerated2</td>
<td>default.domain.com</td>
<td class="centered"> Revoked </td>
<td class="centered">
<td class="centered"> 10/18/2013 19:46:34 GMT </td>
<td class="centered"> 10/17/2016 19:46:34 GMT </td>
<td class="centered">N/A</td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/3',     {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
</td>  
</tr> 
<tr class="alt">
<td>autogenerated3</td>
<td>autogenerated3.domain.com</td>
<td class="centered"> CSR Issued </td>
<td class="centered">
<td class="centered"> 10/18/2013 20:54:55 GMT </td>
<td class="centered"> 10/17/2016 20:54:55 GMT </td>
<td class="centered">1092 </td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/4',   {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
<a onclick="new Ajax.Request('/media/certificates/generate_csr/4',  {asynchronous:true,   evalScripts:true}); return false;" href="#">
<a onclick="new Ajax.Request('/media/certificates/import_certificate_reply/4',    {asynchronous:true, evalScripts:true}); return false;" href="#">
<a onclick="if (confirm('Are you sure you want to revoke this certificate?')) { new   Ajax.Request('/media/certificates/revoke_certificate/4', {asynchronous:true,   evalScripts:true}); }; return false;" href="#">
 </td>
 </tr>
 <tr class="normal">
 <td>Original Certificate</td>
 <td>localhost.localdomain</td>
 <td class="centered"> Self Signed </td>
 <td class="centered">
 <td class="centered"> 10/03/2013 22:37:02 GMT </td>
 <td class="centered"> 10/03/2014 22:37:02 GMT </td>
 <td class="centered">347 </td>
 <td>
  <a onclick="new Ajax.Request('/media/certificates/edit_certificate/1',    {asynchronous:true, evalScripts:true}); return false;" href="#">
  <img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
  </a>
 </td>
 </tr>
  <tr class="alt">
  <td>vhost4</td>
  <td>vhost4.domain.com</td>
  <td class="centered"> Revoked </td>
  <td class="centered">
  <td class="centered"> 10/18/2013 15:58:01 GMT </td>
  <td class="centered"> 10/17/2016 15:58:01 GMT </td>  
  <td class="centered">N/A</td>
  <td>
  <a onclick="new Ajax.Request('/media/certificates/edit_certificate/2',  {asynchronous:true, evalScripts:true}); return false;" href="#">
  <img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
  </a>
  </td>
  </tr>
 </tbody>
</table>

I don't have trouble selecting a icon. Just trouble selecting the correct icon. Both the onclick and image values are selectable items. The icon may not be the last item in the list. I saw a post to try .last.click which does select the last icon in the list. Unfortunately the table posts the data in alphabetical order based on the Alias name. So it may not be the last item in the list and cannot use this method. Suggestions?

b.div(:id, "certificate_table").imgs(:src => "/media/images/icons/edit.gif?1276876449").last.when_present.click
1

1 Answers

1
votes

It sounds like you need to find an element based on its siblings, so you might find this blog post useful. The post gives two options you might consider.

If the text you are looking for is unique - ie the row that has the text is definitely going to be the right row, you can find the td, go to the parent row and then get the link.

b.td(:text => 'autogenerated3').parent.link.click

If you need to ensure that the text is in the first column, then you can do:

b.trs.find{ |tr| 
    tr.td.exists? and tr.td(:index => 0).text == 'autogenerated3'
}.link.click

The tr.td.exists? is added since some of your rows do not have any tds, which would cause an exception when checking the second criteria.