Re: Iterate through rows and cells then click button

85 views
Skip to first unread message

Chuck van der Linden

unread,
Jun 15, 2012, 12:24:54 AM6/15/12
to watir-...@googlegroups.com
On Thursday, June 14, 2012 12:41:31 PM UTC-7, Joshua Homer wrote:
Here is what I am trying to do:

1) go through each row in table
2) go through each cell in rows
3) look for "905" in each cell
4) when "905" is found click on the button with the title "Edit User" in the row

Here is the code I have:

def test_setupUser
@loginPage.login("joshh", "test")
@browser.link(:href, "/admin/admin").click
@browser.table.rows.each do |row|
row.cells.each do |cell|
if cell.text.include? "905"
row.button(:title, "Edit User").click
end
end
end
end

Thank you for your help. 

So the way you have this, it could potentially click Edit User multiple times, is that what you want?

If you just want to find the first row that has a cell that has "905" in its text, then this ought to do what you want

  browser.cell(:text => /905/).parent.button(:title, "Edit User").click

Otherwise why not select just the rows you want and iterate the list, that saves on a lot of testing and sub-iteration of the cells on each row

  browser.table.rows(:text => /905/) do |row|
    row.button(:title, "Edit User").click
  end

 The danger with the one above would be that if you have two cells that somehow combine to form  "905" (from end text in one cell and starting text in another) then you might end up selecting unintended rows, since I can't see your HTML I have no idea if that would be an issue for you or not.

Željko Filipin

unread,
Jun 15, 2012, 3:57:01 AM6/15/12
to watir-...@googlegroups.com
On Thu, Jun 14, 2012 at 9:41 PM, Joshua Homer <countjo...@gmail.com> wrote:
> 1) go through each row in table
> 2) go through each cell in rows
> 3) look for "905" in each cell
> 4) when "905" is found click on the button with the title "Edit User" in the row

I would do it something like this:

browser.td(:text => "905").parent.button(:title => "Edit User").click


> Here is the code I have:

And the problem is?

Željko

Joshua Homer

unread,
Jun 18, 2012, 10:24:10 AM6/18/12
to watir-...@googlegroups.com
Thank you both for the response, I'll try both of these solutions and get back to here with my results.

Joshua Homer

unread,
Jun 18, 2012, 10:33:47 AM6/18/12
to watir-...@googlegroups.com
Here is the code for one of the rows.  This is a single row of about 150 and has the information that I am looking for.

<tr class="{cycle values=" odd,even'}'="">
<td align="center">905</td>
<td align="center">Internal</td>
<td align="center">Automation</td>
<td align="center">TestClient</td>
<td align="center">autotest</td>
<td align="center"><a href="mailto:te...@test.com">te...@test.com</a></td>
<td align="center" nowrap="">
<button type="button" class="iconbutton" style="vertical-align: middle; cursor: default;" onclick="openDialog({name:&quot;Edit User&quot;,
url:&quot;/useradmin/edit/905&quot;, 
submit:&quot;Update User&quot;,
height:680,
width:440,
submit_click:function(){editUserUpdate_symf(&quot;autotest&quot;)}},
this)" title="Edit User">
<img src="/img/user_edit.png" style="vertical-align: middle;">
</button>
    
<button type="button" class="iconbutton" style="vertical-align: middle; cursor: default;" title="Delete User" onclick="if (confirm(&quot;Are you sure you want to delete this user?&quot;)) location.href=&quot;/admin/admin?UserID=905&amp;action=deleteUser&quot;; return false">
<img src="/img/user_delete.png" style="vertical-align: middle;">
</button>
   
<button type="button" class="iconbutton" style="vertical-align: middle; cursor: default;" title="Login As User" onclick="if (confirm(&quot;Are you sure you want to login as this user?&quot;)) location.href=&quot;/admin/admin?UserID=905&amp;action=hijackUser&quot;; return false">
<img src="/img/user_go.png" style="vertical-align: middle;">
</button>
   
</td>
</tr>

Here is the code that I was using to click the button in that row:
@browser.table.trs.each do |row|
if row.tds[0].text == TestUserId
@button = row.button(:title, "Edit User")
@button.click
end
end

This code works, but is extremely slow.  I'll keep you updated on the new code that I'll have from your guys' responses. 

Joshua Homer

unread,
Jun 18, 2012, 10:37:43 AM6/18/12
to watir-...@googlegroups.com
I decided to use this solution:
@browser.td(:text, "905").parent.button(:title, "Edit User").click

Thank you for your help, it took my test time from 90 seconds to 10 :D 

Željko Filipin

unread,
Jun 18, 2012, 11:11:12 AM6/18/12
to watir-...@googlegroups.com
On Mon, Jun 18, 2012 at 4:37 PM, Joshua Homer <countjo...@gmail.com> wrote:
> Thank you for your help, it took my test time from 90 seconds to 10 :D 

Need moar speed? Read this:


Željko

Joshua Homer

unread,
Jun 18, 2012, 1:46:35 PM6/18/12
to watir-...@googlegroups.com
I attempted to implement that, but could not quite get a grasp on it.  My problem was after parsing it I couldn't just do a button.click per se.

I'll take another look at it and see what I can do.
Reply all
Reply to author
Forward
0 new messages