Add and Sum numbers from within box

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Add and Sum numbers from within box

Post by regex » Mon Aug 20, 2012 9:23 pm

I have a form that reports monthly numbers and totals them. I need to count the monthly numbers and compare them to the final tally. I've even had trouble counting the individual rows and summing them using:

[language=]IList<Ranorex.DivTag> myRows = repo.WebDocumentVRad_Portal.NoDiscrepencyRowFour.FindChildren<Ranorex.DivTag>();
Report.Log(ReportLevel.Info,"Number of No Discrepencies for column 'No Discrepencies' = ",myRows.Count.ToString());[/language]

I'd like to count the individual numbers inside each row. Compare them to the final total.

Snapshot is attached.

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

Re: Add and Sum numbers from within box

Post by Support Team » Tue Aug 21, 2012 3:52 pm

Hello,

You can use FindDescendants to get the div tags in your TD tag.
var someTdTag = repo.TryitEditorV15.SomeTableTag1.SomeTdTag;
IList<Ranorex.DivTag> myList = someTdTag.FindDescendants<Ranorex.DivTag>();
foreach(Ranorex.DivTag tag in myList )
{
     Report.Info(tag.GetInnerHtml());
}
Regards,
Bernhard
Ranorex Support Team
.
Image

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Tue Aug 21, 2012 10:01 pm

That worked for grabbing the individual cell. Which is nice.

Yet there are multiple cells in a grid. How do I add each number and compare them to the total row?

Please refer to screenshot.
Attachments
grid.jpg
grid.jpg (10.28 KiB) Viewed 1297 times

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Add and Sum numbers from within box

Post by Ciege » Tue Aug 21, 2012 11:07 pm

In that foreach loop, get the text from each tag var. Switch them to an integer (or some other number type) var and add them all together...

If you want to do a little more you can use searches to determine if the tag you are working with is in the specific column and/or row that you want before doing your addition.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Wed Aug 22, 2012 3:03 pm

Ciege,

Thanks for the response. Do you by chance have some sample code?

A = GetInnerText

GetInnerText.ToInt32()

A + B == Total

What variable from that for each loop is set after it retrieves?

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Add and Sum numbers from within box

Post by Ciege » Wed Aug 22, 2012 3:57 pm

Would need to see a snapshot of your specific element for me to give you specific code... But for some pseudo code to give you an idea of what you can do see below:

Code: Select all

int myTotal = 0;
int myTempInt = 0;
string myTempString = "";
foreach(Ranorex.DivTag tag in myList )  
{  
     Report.Info(tag.GetInnerHtml());  
     myTempString = tag.innerText;
     myTempInt = Convert.ToInt32(myTempString);
     myTotal = myTotal + myTempInt;
}  
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Thu Aug 23, 2012 4:37 pm

Ciege + Bernhard,

You're both lifesavers. Here is the snapshot of the individual column with the screenshot too. The 2 and 3 must be added and compared to the total column. The inner column snapshot file is probably the best snapshot of the individual problem. Here is the code I compiled:

Code: Select all

var reportCell = repo.SelectFacility.reportCell;
IList<Ranorex.DivTag> myList = reportCell.FindDescendants<Ranorex.DivTag>();
foreach(Ranorex.DivTag tag in myList )  
{  
     Report.Info(tag.GetInnerHtml());  

}
Attachments
innerColumbn.png
innerColumbn.png (1.35 KiB) Viewed 1272 times
Last edited by regex on Mon Aug 27, 2012 9:27 pm, edited 1 time in total.

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

Re: Add and Sum numbers from within box

Post by Support Team » Fri Aug 24, 2012 8:26 am

Hi,

can you please provide a snapshot file covering your whole table and not only the specific column?
Because there is not data available within the column as I can see.

Regards,
Tobias
Ranorex Team
.
Image

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Fri Aug 24, 2012 3:34 pm

It is attached.
Attachments
innerGrid.rxsnp
(344.3 KiB) Downloaded 206 times

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Add and Sum numbers from within box

Post by Ciege » Fri Aug 24, 2012 4:25 pm

So it doesn't look like the snapshot contains the all the table, just the 2 TRs. You can see that each TR contains their respective TDs. If you also had the snapshot with the header row you could determine position the "No Discrepancy" column number is (in this case I know it is ChildIndex 4). But you can programatically determine the ChildIndex for that specific column, then iterate through each TR in the table, get that specific TD and then get the data...

So lets assume that we have already found the proper ChildIndex from the header row (4). From there you want to do the following steps...

foreach through each TR in your table.
Get the TD under each TR that has the property ChildIndex=4 (same ChildIndex as the header that you want).
Now get the InnerText property from each DIV under that TR and assign it to a string var.
Convert that string var to an int and add it to you running count.
This will give you the total that you want to then compare to the data in the Total row (also the ChildIndex 4 column).
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Mon Aug 27, 2012 9:28 pm

This should be an adequate snapshot file for this...

Can I still get sample code?
Attachments
snapShot.rxsnp
(444.45 KiB) Downloaded 202 times

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Add and Sum numbers from within box

Post by Ciege » Mon Aug 27, 2012 10:00 pm

OK, first I have to ask... Based on the replies you have already received in this thread and the searching through the forum (that I am sure you have already done - right?), what have you accomplished code wise yourself so far? It is a bit more, ummm cordial, to show that you are indeed trying to do the work yourself and just need a little help along the way rather than asking for others to do your job for you...

That being said... Here is the path I would take to write your code...

1) Do a find single for the "No Discrepancy" TH element ../table/thead/tr/th[@innertext='No Discrepancy'].
2) Get the ChildIndex property of that element and assign that to a variable strChildIndex.
3) Do a find single for the TBody element within that same table.
4) foreach loop through all the the TRs under that TBody.
5) foreach loop through all of the TDs under that TR.
6) if the TD element property = strChildIndex then get the InnerText property of that TD.
7) Add them together.
8 ) Do a find single for the TFoot element within that same table.
9) Again, find the TD that has the ChildIndex = strChildIndex and get the InnerText of that TD.
10) Compare the values you added together from the table with the value you received from the footer.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Wed Aug 29, 2012 7:47 pm

I WAS posting this as a future problem. I wanted to post in preparation for this case when it came up. I solved the problem with my final solution at the very bottom here. It requires 3 classes as explained. I have a refactor idea but without a loop it returns all strings within the cell.

Here's the issue. The first cell has this path:
.//tr[#'ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__1']/td[5]

The second cell has this path:
/dom[@domain='devvprtweb403']//tr[#'ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__0']/td[5]

The only difference is the "ctl00__0" and the "ctl00__1". I have many of these derivative test cases. I put together this code:

(portalqa.portalqaRepository.Instance.SelectFacility.test = .//tr[#'ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00'] )


I wanted to make a variable for the static path and than a variable for each cell that carried the path which changed using this code:

Code: Select all


int myTempInt = 0;
        	String reportCell = ("__0']/td[5]"); 
        	//DivTag wd = repo.RBCorp.BasePath.ToString() + "/" + repo.RBCorp.DetailDetails.BasePath.ToString(); 
       	Ranorex.TrTag tr = portalqa.portalqaRepository.Instance.SelectFacility.test.ToString() + reportCell; 
		//Report.Log(ReportLevel.Info, "Auto", "Evaluate Fine Print Link");
			//Ranorex.TdTag cell = tr.FindSingle(tr).ToString();
			myTempInt = Convert.ToInt32(tr);
			//Report.Info(myTempInt); 
The SelectFacility.Test variable is set so that the cell XPath can be added onto it to fit. This does not compile and gives me a "CellPath does not exist in this context"

-----

I settled on a count using this method. I developed three classes (count, count1, count2). Each count declares an individual cell. I'd like to know if there is an easier way to do this. If anyone has some refactoring ideas. The only thing that comes to mind is calling GetInnerHtml and converting this to an int within a count class like this:

Refactor:

Code: Select all

var reviewTotalCell = repo.ReviewTotalCell.GetInnerHtml();
var reviewTotal = Convert.ToInt32(reviewTotalCell);
firstTotal = reviewTotal(); 

var reviewTotalCell2 = repo.ReviewTotalCell2.GetInnerHtml();
var reviewTotal2 = Convert.ToInt32(reviewTotalCell);
secondTotal = reviewTotal2()

var myTotal = firstTotal + secondTotal;

final count

var FinalCountCell = repo.FinalCountCell.GetInnerHtml();
var reviewTotal = Convert.ToInt32(FinalCountCell); 
if(myTotal == reviewTotal)
        	{
        		Report.Info("Cells match Review Total");
        	}
        	
        	else
        	{
        		Report.Failure("Cells do not match"); 
        	}

----
These are the three currently used count classes

This is count:

Code: Select all

 

public void count()
        {
        	project.projectrespository repo = new project.projectrepository();
        	
        	var firstTotal = Convert.ToInt32(count1());
        	var secondTotal = Convert.ToInt32(count2()); 
        	var myTotal = firstTotal + secondTotal;
        	var totalString = myTotal.ToString(); //works up to here
        	
        	Report.Info(totalString, "This is the total from any cells declared");
        	
        	var reviewTotalCell = repo.ReviewTotalCell.GetInnerHtml();
        	Report.Info(reviewTotalCell, "This is the actual matched value from the TOTAL cell");
        	var reviewTotal = Convert.ToInt32(reviewTotalCell);
        	if(myTotal == reviewTotal)
        	{
        		Report.Info("Cells match Review Total");
        	}
        	
        	else
        	{
        		Report.Failure("Cells do not match"); 
        	}
       
        	
        	
        	
        }
This is count1:

Code: Select all

 public int count1()
        {
        	project.projectrepository repo = new project.projectrepository();
        	
        	
        	        int myTotal = 0;
			int myTempInt = 0;
			string myTempString = "";
			var reportCell = repo.reportCell; 
			IList<Ranorex.DivTag> myList = reportCell.FindDescendants<Ranorex.DivTag>();
			foreach(Ranorex.DivTag tag in myList )  
				{  
     				Report.Info(tag.GetInnerHtml(), "This is the matched value from the first cell");  
     				myTempString = tag.InnerText;
     				myTempInt = Convert.ToInt32(myTempString);
     				//myTotal = otherTotal + myTempInt;
     				
				}
			
			return myTempInt;
        }
This is count2:

Code: Select all

public int count2()
        {
        	project.projectrepository repo = new project.projectrepository();
        	
        	
        	        int myTotal = 0;
			int myTempInt = 0;
			string myTempString = "";
			var reportCell = repo.reportCell2; 
			IList<Ranorex.DivTag> myList = reportCell.FindDescendants<Ranorex.DivTag>();
			foreach(Ranorex.DivTag tag in myList )  
				{  
     				Report.Info(tag.GetInnerHtml(), "This is the matched value from the first cell");  
     				myTempString = tag.InnerText;
     				myTempInt = Convert.ToInt32(myTempString);
     				//myTotal = otherTotal + myTempInt;
     				
				}
			
			return myTempInt;
        }

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

Re: Add and Sum numbers from within box

Post by Support Team » Thu Aug 30, 2012 2:10 pm

Hello,

Thank you for posting the solution!

Regards,
Bernhard
Ranorex Support Team
.
Image

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Add and Sum numbers from within box

Post by regex » Thu Aug 30, 2012 3:11 pm

With a fresh mind this morning, I've realized this is only a partial solution. The data changes with each monthly addition to the reporting structure. I can validate from a static period say July to August which is hard corded in my monthly selection. There are two options to extend this count which I'd like to discuss and find some recommendations.

The data is dynamic and may change. How do I add something to check potential cells which aren't in the repository (string concatenation method I originally worked on) and find the limit to the cells and count everything.

The second extension is making a variable, so a manual tester can call that cell within the argument field, when calling User Code from the GUI. When the variable is added the count prints out and there may be another function which allows cells to be added. This could then be extended or standardized for our project and may help other testers. How would I add the variable into either the existing code, or potentially refactored code to allow this? I think it will take at least a month to solve the second solution while I continue to tinker.

I'd like to use the following code to use variables within the GUI but also deliver those rowTmp strings to a count class within a central code folder. The count class would accept (rowTmp1, rowTmp2) as parameters. I am getting a compile error about this method: code.utility.Count(string rowTmp1, string rowTmp2, string totalTmp); This is intended to send the strings to that class. Any ideas, please?

Code: Select all

 public void countD(string row1, string row2)
        {
            string rowTmp1 = repo.path.GetInnerHtml;
            string rowTmp2 = repo.path.GetInnerHtml;
            string totalTmp = repo.path.GetInnerHtml; 
            
           	code.utility.Count(string rowTmp1, string rowTmp2, string totalTmp);
            	
        }
By the way, here is the increasingly refactored code:

Code: Select all

public void test()
        {
        	project.projectrepository repo = new project.projectrepository();
        	
        	var firstTotal = Convert.ToInt32(trow1());
        	var secondTotal = Convert.ToInt32(trow2()); 
        	var myTotal = firstTotal + secondTotal;
        	var totalString = myTotal.ToString(); //works up to here
        	
        	Report.Info(totalString, "This is the tentative total");
        	
        	var reviewTotalCell = repo.itempath.GetInnerHtml();
        	Report.Info(reviewTotalCell, "This is the actual matched value from the TOTAL cell");
        	var reviewTotal = Convert.ToInt32(reviewTotalCell);
        	if(myTotal == reviewTotal)
        	{
        		Report.Info("Cells match Review Total");
        	}
        	
        	else
        	{
        		Report.Failure("Cells do not match"); 
        	}
       
        	
        	
        }
	  
	  public int trow1()
        {
	  		int myTempInt = 0;
	  		project.projectrepository repo = new project.projectrepository();
        	var reportCell = repo.itempath.GetInnerHtml();
			myTempInt = Convert.ToInt32(reportCell); 
			Report.Info(reportCell, "This is the value from the first cell");
			return myTempInt;
        }
        
	  	public int trow2()
        {
        	int myTempInt = 0;
        	project.projectrepository repo = new project.projectrepository();
        	var reportCell = repo.portal.page.reports.testRow2.GetInnerHtml();
			myTempInt = Convert.ToInt32(reportCell); 
			Report.Info(reportCell, "This is the value from the second cell");
			return myTempInt;
        }