row count and col count

Class library usage, coding and language questions.
sara24
Posts: 4
Joined: Mon Sep 17, 2012 7:16 am

row count and col count

Post by sara24 » Mon Sep 17, 2012 7:27 am

I can use row.count to get a row count right? Can I get an example piece of code to get a row and column count using this documentation

http://www.ranorex.com/Documentation/Ra ... ection.htm

If the Xpath is used? And or the web repository element? Thank you

Code: Select all

public void RowCount()
{
ranorex.webelement testObject = new ranorex.webelement
if(testObject.count() == null)
{
	report.failure("asfd");
}

else
{
	testObject.count(); 
}

	
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: row count and col count

Post by Support Team » Mon Sep 17, 2012 10:32 am

Hi,

Could it be that you want to get the count of the Cells which belong to that row instead of the count of rows in a RowCollection or do you want to get the count of Rows in an IList of rows?
Here is a small example of how you can get the count of cells of a row and of a column and the count of rows in a table of an Excel sheet:
Row row5 = "/form[@processname='EXCEL']/element[@class='XLDESK']/workbook/table[@name='Sheet1']/row[@index='5']";
var columnA = repo.MicrosoftExcelBook1.ColumnA;
Report.Info("Count of Cells in that Row: " + row5.Cells.Count);
Report.Info("Count of Cells in a Column: " + columnA.Cells.Count);
        	
Table sheet1 = "/form[@processname='EXCEL']/element[@class='XLDESK']/workbook/table[@name='Sheet1']";
Report.Info("Count of Rows in a Table: " + sheet1.Rows.Count);
Regards,
Markus
Ranorex Support Team
.
Image

sara24
Posts: 4
Joined: Mon Sep 17, 2012 7:16 am

Re: row count and col count

Post by sara24 » Mon Sep 17, 2012 12:18 pm

Thank you. But it's an HTML table.

Yes I want to get the count of cells which belong to that row. But I also want to do it with an IList? Maybe share both?

Code: Select all

arraylist rowCount = new arraylist(); 
rowCount.add(Xpath or repository path.toString()) 
repo.td.count? or cells.count?  

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: row count and col count

Post by Aracknid » Mon Sep 17, 2012 2:28 pm

I've been looking for an easier way for Row, Col and Cell values for an HTML table (which is not the same as a Table) but have been using this method below...
Dim MyTable as Ranorex.TableTag

MyTable = "xxxxxxxxx" <-- path to table... not important

NumRows = MyTable.FindSingle("./tbody").as(of Ranorex.TbodyTag).Children.Count
NumCols = MyTable.FindSingle("./tbody/tr[1]").as(of Ranorex.TRTag).Children.Count 'For row 1
CellValue = MyTable.FindSingle("./tbody/tr[1]/td[3]").as(of Ranorex.TDTag).InnerText 'For row 1 col 3

sara24
Posts: 4
Joined: Mon Sep 17, 2012 7:16 am

Re: row count and col count

Post by sara24 » Mon Sep 17, 2012 3:38 pm

I like your solution Aracnid.

In C Sharp that would be

Code: Select all

Ranorex.TableTag = myTable; 
myTable = "xx"; '
NumRows = myTable.FindSingle.....Children.Count
I have this but there's a problem when I debugged. The "grid.FindDescendants<Ranorex.TrTag>(); " element is not holding the XPATH that is passed via the location parameter. These are helpful comments but I also wonder why this is happening.

Code: Select all

public static void RowCount(string location)
		{
			 
			Ranorex.Element grid = null;
			bool lowerLevelException = true; 
			 
			
			try
			{
				grid = string.Format(location.ToString()); 
			}
			
			catch(Exception e)
			{
				lowerLevelException = false;  
			}

			if(lowerLevelException)
			{
			
			
			// get all rows in table and store them in a list
			IList<Ranorex.TrTag> myRows = grid.FindDescendants<Ranorex.TrTag>();  
			// print out count of rows stored in a list   
			Report.Log(ReportLevel.Info,"Row Count: ", myRows.Count.ToString());  
			
			}
			
		}

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: row count and col count

Post by sham526 » Tue Sep 18, 2012 6:53 am

Using ILIST(VB.Net):

Dim temp_td As TdTag = Nothing
Dim bool As Boolean = Host.Local.TryFindSingle(".//td[@id='ReportCell']", temp_td)
'For ROW Count
Dim list_trtag As IList(Of TrTag) = temp_td.Find(Of TrTag)("./table/tbody/tr")
MsgBox("The Row count Found was : " + list_trtag.Count.ToString)
'Now Get the column count for each row.
For i as integer = 1 to list_trtag.count
Dim Each_trtag As TrTag = temp_td.Findsingle(Of TrTag)("./table/tbody/tr["+i.ToString+"]")
Dim list_tdtag As IList(Of TdTag) = Each_trtag.Find(Of TdTag)("./td")
MsgBox("The Column count For ROW : "+i.ToString+" was : " + list_tdtag .Count.ToString)
Next

sara24
Posts: 4
Joined: Mon Sep 17, 2012 7:16 am

Re: row count and col count

Post by sara24 » Wed Sep 19, 2012 4:34 am

Using the CSharp IList, how do I get the 3-5 cells from that count? And then...get the next row of 3rd through 5th cells :)

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: row count and col count

Post by Support Team » Wed Sep 19, 2012 3:58 pm

Hi,

You can access an element on a specific position as you would do it with normal arrays:
Table table = "";
            
            // get all Rows from Table
            IList<Row> iList = table.Rows;
            
            // get Cells of the fourth Row
            IList<Cell> cellList = iList[3].Cells;
            
            //get third Cell:
            Report.Info("Text of third Cell: "+cellList[2].Text);
Here is a link to the API of a .Net List: List<T> Class.

Regards,
Markus
Ranorex Support Team
.
Image

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: row count and col count

Post by Aracknid » Wed Sep 19, 2012 4:00 pm

Does that work with HTML tables?

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: row count and col count

Post by sham526 » Thu Sep 20, 2012 6:29 am

Hi Sara24,
i converted the code from vb.net to C#.Net, and it gave below.Just Try this.
For all the rows present in the table it will get u the 3,4,5 column data.
TdTag temp_td = null;
bool @bool = Host.Local.TryFindSingle(".//td[@id='ReportCell']", temp_td);
IList<TrTag> list_trtag = temp_td.Find<TrTag>("./table/tbody/tr");
for (int i = 1; i <= list_trtag.Count; i++) {
TdTag Third_tdtag = temp_td.FindSingle<TdTag>("./table/tbody/tr[" + i.ToString() + "]/td[3]");
U can use this object and access the methods
TdTag Fourth_tdtag = temp_td.FindSingle<TdTag>("./table/tbody/tr[" + i.ToString() + "]/td[4]");
TdTag Fifth_TdTag = temp_td.FindSingle<TdTag>("./table/tbody/tr[" + i.ToString() + "]/td[5]");
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: row count and col count

Post by Support Team » Thu Sep 20, 2012 5:03 pm

Hi,

No, my code doesn't work for HTML tables, the following code would do the same, just for HTML tables ;):
TableTag simpletable = repo.RanorexTestPage.Simpletable;
                  
                // get all Rows from Table  
                IList<TrTag> iList = simpletable.FindDescendants<TrTag>();
                  
                // get Cells of the fourth Row  
                IList<TdTag> cellList = iList[3].FindDescendants<TdTag>();  
                  
                //get third Cell:  
                Report.Info("Text of third Cell: "+cellList[2].InnerText);
Regards,
Markus
Ranorex Support Team
.
Image