Compare Files (like CSV)
Posted: Wed Feb 07, 2018 11:08 am
Hello everyone,
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.
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!");
}
}