Page 1 of 1

How to loop through rows in a table

Posted: Thu Apr 15, 2010 4:52 pm
by markvanraalte
Hi

I am trying to automate a Java web app.
There is a table in the application (that I want to control) which contains a number of rows (with about 8 columns).

I wish to be able to loop through the rows in C# and then click on the first column if it contains a certain "innertext" value in the third column.

I am new to Ranorex and am not sure how to do this.

The Ranorex path for the first row (and first text column) is as follows:

"/dom[@caption='PureDeal']/body/div[@id='dragArea']/div[@id='divOpenPositions']/div[@id='ifrOpenPositionsHolder']/iframe/body/div/div/table/tbody[2]/tr[@id='yui-rec11']/td[2]/div/a[@innertext='CellTextHere']"

Now I can replace the expression: @id='yui-rec11' with the number 1 for first row
but I when I remove the innertext expression it no longer finds the cell.

I wish to loop thru all rows and read the values of innertext in columns 1 and 3. If the match a criteria I wish to click on column 1.

Please help
Thanks

Re: How to loop through rows in a table

Posted: Fri Apr 16, 2010 11:01 am
by Support Team
Hi!

What your are looking for is the foreach statment in C#.
markvanraalte wrote:I wish to loop thru all rows and read the values of innertext in columns 1 and 3. If the match a criteria I wish to click on column 1.
You should code something like:
foreach (TrTag row in repo.Sample.TableBody.Find("./tr"))
{
   IList<TdTag> cells = row.Find<TdTag>("./td");
   if (cells.Count >= 3
         && cells[0].InnerText == "SomeValue"
         && cells[2].InnerText == "SomeOtherValue")
   {
      cells[0].Click();
   }
}
Regards,
Peter
Ranorex Support Team

Re: How to loop through rows in a table

Posted: Fri Apr 16, 2010 7:14 pm
by markvanraalte
Thanks Peter

Thats really helpful.

Unfortunately I needed to reference tags that were lower down (within the /td element)

I solved it as follows:
foreach (TrTag row in repo.WebDocumentPureDeal.OpenPositionsWindow.TableBody.Find("./tr"))
{
IList<TdTag>cells = row.Find<TdTag>("./td");
ATag _securityCell = cells[0].Children[0].FindSingle("./a");
DivTag _positionCell = cells[2].FindSingle("./div");

if ("SomeValue" == _securityCell.InnerText
&& "AnotherValue" == _positionCell.InnerText)

etc

Presumably this is a reasonable thing to do? It seems to work fine.

Re: How to loop through rows in a table

Posted: Mon Apr 19, 2010 6:07 pm
by Support Team
markvanraalte wrote:Presumably this is a reasonable thing to do? It seems to work fine.
That's exactly how I would do it :-)

Regards,
Alex
Ranorex Support Team