Compare Files (like CSV)

Best practices, code snippets for common functionality, examples, and guidelines.
diogogmaio
Posts: 33
Joined: Thu May 19, 2016 4:52 pm

Compare Files (like CSV)

Post by diogogmaio » Wed Feb 07, 2018 11:08 am

Hello everyone, :D

I have multiple modules to compare CSV and Excel files but sometimes the validation only checks if the files have the same size/lines/columns/values...accuracy.

But i want a deeper report. Something like the Compare plugin from Notepad++.
In this case, if only one line is missing from one of the compared files i dont want to see a message like "THe files dont match".

I want Ranorex compare module to also highlight that it's only a missing line and keep comparing all the remaining values.

So basically the compare plugin from notepad++.

What should be my approach?
WHat can i do to achieve something like this?

If someone can help me with some code examples, i would be so happy.

My csv code comparison module (simple >> compare files size, lines and values) >> but will stop comparison if one line is missing and will not compare all the values in the files after that.

Code: Select all

		public static void CompareCSVFiles(string refFile, string cmpFile)
		{
			//validate path to configuration file
			TestFileExists(refFile);
			//create CSV data connector
			string refConnector = "CSVConnector";

			TestFileExists(cmpFile);
			//create CSV data connector
			string cmpConnector = "CSVConnector";

			//get data from ref. CSV
			Ranorex.Core.Data.CsvDataConnector refCSVConnector = new Ranorex.Core.Data.CsvDataConnector(refConnector,@refFile,false);
			refCSVConnector.SeparatorChar = ',';
			Ranorex.Core.Data.ColumnCollection refCSVColumns = new Ranorex.Core.Data.ColumnCollection();
			Ranorex.Core.Data.RowCollection refCSVRows = new Ranorex.Core.Data.RowCollection(refCSVColumns);
			
			//load CSV connector
			refCSVConnector.LoadData(out refCSVColumns, out refCSVRows);

			//get data from cmp. CSV
			Ranorex.Core.Data.CsvDataConnector cmpCSVConnector = new Ranorex.Core.Data.CsvDataConnector(cmpConnector,@cmpFile,false);
			cmpCSVConnector.SeparatorChar = ',';
			Ranorex.Core.Data.ColumnCollection cmpCSVColumns = new Ranorex.Core.Data.ColumnCollection();
			Ranorex.Core.Data.RowCollection cmpCSVRows = new Ranorex.Core.Data.RowCollection(cmpCSVColumns);
			
			//load CSV connector
			cmpCSVConnector.LoadData(out cmpCSVColumns, out cmpCSVRows);
			
			Ranorex.Core.Data.Row refRowCSV;
			Ranorex.Core.Data.Row cmpRowCSV;
			if (refCSVRows.Count == cmpCSVRows.Count)
			{
				//go through ref/cmp CSV files and compare individual elements
				string refCSVValue = "";
				string cmpCSVValue = "";
				bool differenceFound = false;
				for (int i=0; i<=refCSVRows.Count-1; i++)
				{
					refRowCSV = refCSVRows[i];
					cmpRowCSV = cmpCSVRows[i];
					for (int j=0; j<=refCSVColumns.Count-1; j++)
					{
						refCSVValue = refRowCSV[j].ToString();
						cmpCSVValue = cmpRowCSV[j].ToString();
						if (refCSVValue != cmpCSVValue)
						{
							Report.Log(ReportLevel.Failure, "Comparison value different than reference value...", "Reference value: " + refCSVValue + "\n" + "Comparison value: " + cmpCSVValue);
							differenceFound = true;
						}
					}
				}
				if (!differenceFound)
				{
					Report.Log(ReportLevel.Success, "Validation", "Validation OK! Reference and compare CSV files the same!");
				}
			}
			else
			{
				// skip the iteration in case the number of ref and cmp rows differ
				throw new RanorexException("Number of rows in cmp. CSV file is not equal to number of rows in ref. CSV file!");
			}
		}

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

Re: Compare Files (like CSV)

Post by odklizec » Wed Feb 07, 2018 12:51 pm

Hi,

I'm afraid, doing comparison like in notepad++ would be too complicated. I mean coding it from scratch. There may be a comparison library, which may be useful in .net code? But for me, it was enough to see differences in files with the same number of rows. And all results with different number of rows I consider a failure. I don't care about different results in individual rows if the number of rows is already different ;) You see, even if you somehow get the missing/redundant rows, displaying them in report would be a mess. So it's much easier and nicer to evaluate the differences in a dedicated comparison tool.
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

diogogmaio
Posts: 33
Joined: Thu May 19, 2016 4:52 pm

Re: Compare Files (like CSV)

Post by diogogmaio » Wed Feb 07, 2018 12:55 pm

I am using like a Ranorex + DiffMerge combo because i need to see the differences between the compared files and extract them to a file/report.
Can't just compare them because sometimes one line is missing but the rest of the file is 100% ok.
I need to isolate the line and discover why it's missing on the other report.

Basically that.
Sadly that i cant make a native stuff on ranorex for that.

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

Re: Compare Files (like CSV)

Post by odklizec » Wed Feb 07, 2018 1:09 pm

Well, you can do (almost) anything you want with Ranorex. It's only a question of time and energy you are prepared to invest in this task ;) With proper coding, you can do many things. So if I were you, I would first search for an available comparison library/method, which you can include in your project. And if nothing suitable is found in .net word, you will have to write something yourself.
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