How to pass Ranorex snapshot to validate table

Best practices, code snippets for common functionality, examples, and guidelines.
mayank9292
Posts: 6
Joined: Fri Sep 12, 2014 4:26 pm

How to pass Ranorex snapshot to validate table

Post by mayank9292 » Tue Dec 09, 2014 2:47 pm

Hi,

I used this code to validate table


const string NullString = "(null)";
public void Validate_TableContentEqual(Ranorex.Adapter repoItem, string filename_ReferenceTableSnapshot, string customLogMessage)
{
// cast repoItem to Table
Ranorex.Table table = repoItem.As <Ranorex.Table>();

// check if snapshot file exists
const string fileNotExists = "The given file does not exist: {0}";
if (!System.IO.File.Exists(filename_ReferenceTableSnapshot))
{
throw new Ranorex.RanorexException(string.Format(fileNotExists, filename_ReferenceTableSnapshot));
}

// restore table from snapshot
var snap = Ranorex.Core.ElementSnapshot.DeserializeXml(System.IO.File.ReadAllText(filename_ReferenceTableSnapshot));
Ranorex.Table refTable = null;
try
{
refTable = snap.Element;
}
catch
{
throw new Ranorex.RanorexException("Table could not be created from snapshot");
}
var tableAdapter = table.As<Ranorex.Table>();
if (tableAdapter==null)
{
throw new Ranorex.RanorexException("Table could not be restored from Snapshot");
}

// check if rowcount is identical
if (tableAdapter.Rows.Count != refTable.Rows.Count)
{
throw new Ranorex.RanorexException(String.Format ("Tables do not have same number of rows ({0} vs. {1})", tableAdapter.Rows.Count, refTable.Rows.Count));
}

// run through table-rows
for (int iRow = 0; iRow <= tableAdapter.Rows.Count - 1; iRow++)
{
int cellCountCur = tableAdapter.Rows[iRow].Cells.Count;
int cellCountRef = refTable.Rows[iRow].Cells.Count;

// check if number of cells is identical in current row
if (cellCountCur != cellCountRef)
{
throw new Ranorex.RanorexException(String.Format("Table-Row do not have same number of cells ({0} vs. {1})", cellCountCur, cellCountRef));
}

// run through cells in current row
for (int iCol = 0; iCol <= cellCountCur - 1; iCol++)
{
string aCurText = tableAdapter.Rows[iRow].Cells[iCol].As<Ranorex.Cell>().Text;
string aRefText = refTable.Rows[iRow].Cells[iCol].As<Ranorex.Cell>().Text;
if (customLogMessage.Trim().Equals(string.Empty) || customLogMessage.Trim().Equals (NullString) )
{
customLogMessage = "Comparing cells: actual value = {0}, expected value = {1}";
}

// validate whether current text and expected text are identical
Ranorex.Validate.AreEqual (aCurText, aRefText, customLogMessage);
}
}
// Log success
Ranorex.Report.Log (ReportLevel.Success, customLogMessage);
}




and passed the path of Ranorex snapshot in the user code but while running it is giving error as "Document must not contain #text nodes as children( on the line which is in bold) . " i have attached the Ranorex Snapshot also. Please help
Attachments
myTable.zip
contains Ranorex snapshot
(879.24 KiB) Downloaded 136 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to pass Ranorex snapshot to validate table

Post by odklizec » Tue Dec 09, 2014 3:31 pm

Hi,

I would suggest you to check this code sample (table validation using snapshot):
http://www.ranorex.com/support/user-gui ... html#c7781
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mayank9292
Posts: 6
Joined: Fri Sep 12, 2014 4:26 pm

Re: How to pass Ranorex snapshot to validate table

Post by mayank9292 » Tue Dec 09, 2014 3:37 pm

Hi,

I already tried with the example and mentioned the failure message from the report in the post.
i want to know why it is giving failure message.

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

Re: How to pass Ranorex snapshot to validate table

Post by Support Team » Wed Dec 10, 2014 9:42 am

Hello Mayank,

in the code examples - where your code snippet is taken from - there is a note that should be read carefully. It points out that Ranorex Snapshot files are compressed (zip archive) and need to be extracted first in order to use it in the validation. It seems that using the uncompressed snapshot file causes the mentioned error message ("Document must not contain #text nodes as children")!

Furthermore, this code example is intended to test against "common tables" and not against tables in web. Tables in web are structured differently (THead, TBody, TR, TD, ...) than tables in common desktop applications (Table, Row, Cell).
I've created a new code example to validate against web tables. This sample is quite similar to the one linked above but takes account of the different table structure.

Kind regards
Roland (E)
Ranorex Team
.
Image

mayank9292
Posts: 6
Joined: Fri Sep 12, 2014 4:26 pm

Re: How to pass Ranorex snapshot to validate table

Post by mayank9292 » Wed Dec 10, 2014 10:54 am

Hi Roland,

My snapshot is in the form of "*.rxsnp". How should i extract it because it not giving any option to extract.
Please check my attached Ranorex snapshot the earlier post.
Furthermore, I am not able to see your new solution as it is not showing in the examples.
Can you attach the solution with reply.

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

Re: How to pass Ranorex snapshot to validate table

Post by Support Team » Wed Dec 10, 2014 11:08 am

Hi,
My snapshot is in the form of "*.rxsnp". How should i extract it because it not giving any option to extract.
Even rxsnp files can be extracted. I use 7-Zip but I’m pretty sure, other tools are able to do so as well.
The archive contains a file with the same file name – so during extraction please provide another file name or path.
Furthermore, I am not able to see your new solution as it is not showing in the examples.
Please refresh your browser, clear cache, restart your browser or use another browser. Here's the link again: Link

Kind regards,
Roland (E)
Ranorex Team
.
Image

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to pass Ranorex snapshot to validate table

Post by odklizec » Tue Mar 24, 2015 5:30 pm

Support Team wrote: I've created a new code example to validate against web tables. This sample is quite similar to the one linked above but takes account of the different table structure.

Kind regards
Roland (E)
Ranorex Team
Hi Roland,

I'm just in a need to validate whole HTML table and I thought it would be good to use the snapshot comparison approach. So far, I validated the HTML tables row by row, using CVS reference files, but it seems to be impractical for larger tables. So I wanted to try your sample, but it seems to be gone? ;) Any chance to have it back? Thanks in advance.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Re: How to pass Ranorex snapshot to validate table

Post by CookieMonster » Wed Mar 25, 2015 3:44 pm

Hi all,

I have a question, why are you validating a repository file against the actual table? Why are you not saving the actual table with its row, columns, values and attributes into a an xml file.
Then you can use XPathNavigator and you can validate the table with XPath.

Regards
Dan

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to pass Ranorex snapshot to validate table

Post by odklizec » Thu Mar 26, 2015 2:36 pm

Hi Dan,

We are not trying to compare repository file but Ranorex Snapshot file, which holds the structure of GUI in XML file. This file can be created either manually (from spy) or calling a method from code. So it's very easy to create GUI reference files. I think it's a great idea to use snapshots for validation. I personally don't have a clue how to use XPathNavigator and to be quite honest, I would prefer to use Ranorex own tools and methods wherever possible. This is why I would like to see the code sample mentioned in Roland's post and which seems to be gone now ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to pass Ranorex snapshot to validate table

Post by odklizec » Fri Mar 27, 2015 11:19 am

OK, I think I figured it out myself ;) I was able to adapt the existing "common table" validation sample for HTML tables. Here is the code (just in case anyone needs this too):
const string NullString = "(null)";
        public void Validate_TableContentEqual(Ranorex.Adapter repoItem, string filename_ReferenceTableSnapshot, string customLogMessage)
        {
        	// cast repoItem to Table
        	Ranorex.TableTag table = repoItem.As <Ranorex.TableTag>();
        	
        	// check if snapshot file exists
        	// don't forget the snapshot needs to be extracted (unzipped) before being used!!!
        	const string fileNotExists = "The given file does not exist: {0}";
        	if (!System.IO.File.Exists(filename_ReferenceTableSnapshot))
        	{
        		throw new Ranorex.RanorexException(string.Format(fileNotExists, filename_ReferenceTableSnapshot));
        	}
        	
        	// restore table from snapshot
        	var snap = Ranorex.Core.ElementSnapshot.DeserializeXml(System.IO.File.ReadAllText(filename_ReferenceTableSnapshot));
        	Ranorex.TableTag refTable = null;
        	try
        	{
        		refTable = snap.Element;
        	}
        	catch
        	{
        		throw new Ranorex.RanorexException("Table could not be created from snapshot");
        	}
        	var tableAdapter = table.As<Ranorex.TableTag>();
        	if (tableAdapter==null)
        	{
        		throw new Ranorex.RanorexException("Table could not be restored from Snapshot");
        	}
        	
        	// check if rowcount is identical
        	IList<TrTag> iListCurRows = tableAdapter.FindDescendants<TrTag>();
        	IList<TrTag> iListRefRows = refTable.FindDescendants<TrTag>();
        	int rowCountCur = iListCurRows.Count;
        	int rowCountRef = iListRefRows.Count;
        	if (rowCountCur != rowCountRef)
        	{
        		throw new Ranorex.RanorexException(String.Format ("Tables do not have same number of rows ({0} vs. {1})", rowCountCur, rowCountRef));
        	}
       	
        	// run through table-rows
        	foreach (TrTag curRow in iListCurRows) 
        	{
        		IList<TdTag> iListCurRowCells = curRow.FindDescendants<TdTag>();
        		TrTag rowRef = iListRefRows[iListCurRows.IndexOf(curRow)];
        		IList<TdTag> iListRefRowCells = rowRef.FindDescendants<TdTag>();
        		
        		int cellCountCur = iListCurRowCells.Count;
        		int cellCountRef = iListRefRowCells.Count;
        		// check if number of cells is identical in current row
        		if (cellCountCur != cellCountRef)
        		{
        			throw new Ranorex.RanorexException(String.Format("Table-Rows do not have same number of cells ({0} vs. {1})", cellCountCur, cellCountRef));
        		}
        		
        		// run through cells in current row
	        	foreach (TdTag curCell in iListCurRowCells) 
    	    	{
        			string cellInnerTextCur = curCell.InnerText;
        			string cellInnerTextRef = iListRefRowCells[iListCurRowCells.IndexOf(curCell)].InnerText;
        			if (customLogMessage.Trim().Equals(string.Empty) || customLogMessage.Trim().Equals(NullString) )
        			{
        				customLogMessage = "Comparing cells: actual value = {0}, expected value = {1}";
        			}
        			
        			// validate whether current text and expected text are identical
        			Ranorex.Validate.AreEqual(cellInnerTextCur, cellInnerTextRef, customLogMessage);        			
	        	}
        	}
        	// Log success
        	Ranorex.Report.Log (ReportLevel.Success, "OK");
        }
I'm not quite sure my adapted code is the best way, but it seems it works for my tables ;) Feel free to make changes you think would make the code more efficient.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

nisha
Posts: 6
Joined: Thu Jun 25, 2015 8:23 am

Compilation errors in "Advance validation- whole table" code

Post by nisha » Thu Aug 06, 2015 12:34 pm

Ranorex_errors.PNG
screenshot with error
Ranorex_errors.PNG (248.06 KiB) Viewed 2244 times
PreviewImpactrxsnp.rxsnp
table_snapshot
(190.37 KiB) Downloaded 67 times
Hi Ranorex,

I'm tring to validate the validate a whole table in Ranorex.

Copy pasted the "Advance validation -whole table" code in a usercode module. keep getting 2 compilation errors when playing the "user code".Below are the error messages.when googled,it shows a parentheses is expected.checked all the loops,everything is right.I don't understand the reason for the errors.

Errors:
Type or namespace definition, or end-of-file expected (CS1022)
} expected (CS1513)

attached snapshot and required screenshot
Attachments
Ranorex_errors.PNG
Compilation error
Ranorex_errors.PNG (206 KiB) Viewed 2244 times

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

Re: How to pass Ranorex snapshot to validate table

Post by Support Team » Mon Aug 10, 2015 12:23 pm

Hi nisha,

We have updated the code in these examples and it should now work as expected. May I ask you if you can try the latest example?

Please also note that the snapshot file needs to be extracted first. Therefore, you could use 7-Zip or any other tool, which can extract archives. The archive itself contains a file with the same name - so please extract it to another location.

Please let me know if you were able to overcome the problem. Otherwise, I'd need your whole solution to analyze the issue on my machine.

Regards,

Markus (S)
.
Image

nisha
Posts: 6
Joined: Thu Jun 25, 2015 8:23 am

Re: How to pass Ranorex snapshot to validate table

Post by nisha » Mon Aug 10, 2015 1:57 pm

Hi Markus,
Thank you for your response.
I extracted the snapshot using 7-zip and stored the extracted filename in different name(open archive->extract->auto rename).Even after extracting the file extension is .rxsnp
In the Ranorex snapshot ,the grid is recognized as "unknown" element. Ranorex not able to identify it as "Table".
Our application under test is built on Delphi and tables layout are based on Devexpress grid. I've got reply from Georg Kapeller(Ranorex support team) that Ranorex doesn't provide much support on Delphi application.
we want to use Ranorex mainly for Regression testing of our application and it includes mainly of comparing whole table values for every release.

Regards,
Nisha.

nisha
Posts: 6
Joined: Thu Jun 25, 2015 8:23 am

Re: How to pass Ranorex snapshot to validate table

Post by nisha » Mon Aug 10, 2015 3:51 pm

hi Markus,

I tried with the new code in the code examples.

Extracted zip file using 7 zip and saved it in the same folder with same name(and renamed the original file).But only error message "The given file doesn't exists".

In the "filename_ReferenceTableSnapshot" , Do I need to send the full path or just a file name ?I tried both, but no luck.

e.g. If my extracted snapshot filename is "Hardflagimpact.rxsnp" and saved in the Ranorex\Snapshots" folder. what is the value for "filename_ReferenceSnapshot" variable.

1.Hardflagimpact.rxsnp
2.Hardflagimpact
3.C:\Users\nsiv\Documents\Ranorex\Snapshots
4.C:\Users\nsiv\Documents\Ranorex\Snapshots\Hardflagimpact.rxsnp
5.C:\Users\nsiv\Documents\Ranorex\Snapshots\Hardflagimpact

I tried all possible combinations, but receiving same error. With the previous code it was working fine but received error message "Table could not be created from snapshot" which is actually a limitation in Ranorex on Devexpress grid.

nisha
Posts: 6
Joined: Thu Jun 25, 2015 8:23 am

Re: How to pass Ranorex snapshot to validate table

Post by nisha » Mon Aug 10, 2015 4:18 pm

Hi Markus,

With new code, able to send the file path name in the "filename_ReferenceSnapshot"(C:\Users\nsiv\Documents\Ranorex\Snapshots\Hardflagimpact.rxsnp),but it ended up in the error "Table could not be created from snapshot".