I am trying to use Core.Comparator.Pdf.Compare that comes with Acumatica Test SDK
Below sample code that should compare PDFs created from the same report but for different shipments.
MagickNET.SetGhostscriptDirectory(@"C:\Program Files (x86)\gs\gs10.03.1\bin");
var file1 = new FileInfo("C:\\Users\\abaranov\\source\\repos\\gitlab-hemasource\\HSAutomatedTests\\HSAutomatedTests\\bin\\Debug\\SO642003_689445.pdf");
var file2 = new FileInfo("C:\\Users\\abaranov\\source\\repos\\gitlab-hemasource\\HSAutomatedTests\\HSAutomatedTests\\bin\\Debug\\SO642003_689455.pdf");
Comparator.Pdf.Compare(file1, file2);
Comparator executes without error, but all I see in the log is just saying there is data mismatch (see screenshot)
Is it all I should be hoping for?
I expected images of both files with highlighted regions showing where exactly mismatch is
Best regards,
Andrey
Best answer by abaranovhs
The issue I found is because TestSDK Comparator class “saves” result of comparison using Dummy method
I created my own class that runs report(s) and takes screenshot if PDF differs from the baseline file.
Putting code here just in case if someone else wants it (note, this requires installing Ghostscript on the computer where test runs, and specifying its location using MagicNET.SetGhostScriptDirectory method):
publicclass RunReport : Wrappers.ReportLauncher
{
publicRunReport(string screenID)
{
base.ScreenId = screenID;
base.ScreenUrl = $"/frames/reportlauncher.aspx?id={screenID}.rpx";
ToolBar = new PxToolBar(null);
}
public c_parameters ReportParameters => Parameters;
publicstatic (FileInfo, FileInfo) RunReportAndExportPDF(RunReport report, string baselineFileName)
{
FileInfo downloadedFile;
Log.Information("Baseline file used: " + baselineFileName);
report.Run();
Wait.WaitForPageToLoad();
report.HtmlMode();
Wait.WaitForPageToLoad();
report.Export();
report.PDF();
Wait.WaitForFileDownloadComplete();
downloadedFile = Core.Core.Browser.Browser.Downloads.GetLastFile();
baselineFileName = AppDomain.CurrentDomain.BaseDirectory + baselineFileName;
var file1 = downloadedFile;
var file2 = new FileInfo(baselineFileName);
return (file1, file2);
}
publicstaticvoidRunPDFCompare(FileInfo file1, FileInfo file2)
{
Log.Information($"Comparing files {file1.Name}, {file2.Name}");
string outputPicturePath = ConfigExtended.LogSettings.LogFolder + "\\picture.png";
MagickNET.SetGhostscriptDirectory(@"C:\Program Files (x86)\gs\gs10.03.1\bin");
var mi1 = new MagickImage(file1, MagickFormat.Pdf);
var mi2 = new MagickImage(file2, MagickFormat.Pdf);
bool differencesFound = false;
using (var diffImage = new MagickImage() { ColorFuzz = new Percentage(10) })
{
differencesFound = mi1.Compare(mi2, new CompareSettings() { Metric = ErrorMetric.Absolute }, diffImage) > 0;
diffImage.Write(outputPicturePath, format: MagickFormat.Png); // Save the diff image to disk
}
if (differencesFound)
{
Browser.WebDriver.Navigate().GoToUrl(outputPicturePath);
Log.Error("Differences found");
}
else
{
Log.Information($"Files are identical");
}
File.Delete(outputPicturePath);
file1.Delete();
}
}
Example of the test:
[TestDescription("\r\nThis test runs varios reports and compares them with the baseline pre-saved files")]
internalclass TestPDFReports : CheckExt
{
privatestring description;
publicoverridevoidExecute()
{
description = "[Test 1]\r\n";
description += "Run Shipment Packing Slip SO642003, compare with saved baseline" + "\r\n";
using (TestExecution.CreateTestCaseGroup(description))
{
string reportID = "SO642003";
string shipmentNbr = "689455";
//Specify path to the file saved in HSAutomatedTests project folder, that will be used as a baseline to compare generated report tostring baselineFileName = @"ReportBaseLines\SO642003_689455.pdf";
RunReport report = new RunReport(reportID);
report.OpenScreen();
report.ReportParameters.DynamicControl<Selector>("Shipment Nbr.").Select(shipmentNbr);
(FileInfo file1, FileInfo file2) = RunReport.RunReportAndExportPDF(report, baselineFileName);
RunReport.RunPDFCompare(file1, file2);
}
description = "[Test 2]\r\n";
description += "Run Shipment Packing Slip SO642003, compare with saved baseline" + "\r\n";
using (TestExecution.CreateTestCaseGroup(description))
{
string reportID = "SO642003";
string shipmentNbr = "689455";
//Specify path to the file saved in HSAutomatedTests project folder, that will be used as a baseline to compare generated report tostring baselineFileName = @"ReportBaseLines\SO642003_689445.pdf";
RunReport report = new RunReport(reportID);
report.OpenScreen();
report.ReportParameters.DynamicControl<Selector>("Shipment Nbr.").Select(shipmentNbr);
(FileInfo file1, FileInfo file2) = RunReport.RunReportAndExportPDF(report, baselineFileName);
RunReport.RunPDFCompare(file1, file2);
}
}
}
Screenshot showing how baseline files are configured in the solution
The issue I found is because TestSDK Comparator class “saves” result of comparison using Dummy method
I created my own class that runs report(s) and takes screenshot if PDF differs from the baseline file.
Putting code here just in case if someone else wants it (note, this requires installing Ghostscript on the computer where test runs, and specifying its location using MagicNET.SetGhostScriptDirectory method):
publicclass RunReport : Wrappers.ReportLauncher
{
publicRunReport(string screenID)
{
base.ScreenId = screenID;
base.ScreenUrl = $"/frames/reportlauncher.aspx?id={screenID}.rpx";
ToolBar = new PxToolBar(null);
}
public c_parameters ReportParameters => Parameters;
publicstatic (FileInfo, FileInfo) RunReportAndExportPDF(RunReport report, string baselineFileName)
{
FileInfo downloadedFile;
Log.Information("Baseline file used: " + baselineFileName);
report.Run();
Wait.WaitForPageToLoad();
report.HtmlMode();
Wait.WaitForPageToLoad();
report.Export();
report.PDF();
Wait.WaitForFileDownloadComplete();
downloadedFile = Core.Core.Browser.Browser.Downloads.GetLastFile();
baselineFileName = AppDomain.CurrentDomain.BaseDirectory + baselineFileName;
var file1 = downloadedFile;
var file2 = new FileInfo(baselineFileName);
return (file1, file2);
}
publicstaticvoidRunPDFCompare(FileInfo file1, FileInfo file2)
{
Log.Information($"Comparing files {file1.Name}, {file2.Name}");
string outputPicturePath = ConfigExtended.LogSettings.LogFolder + "\\picture.png";
MagickNET.SetGhostscriptDirectory(@"C:\Program Files (x86)\gs\gs10.03.1\bin");
var mi1 = new MagickImage(file1, MagickFormat.Pdf);
var mi2 = new MagickImage(file2, MagickFormat.Pdf);
bool differencesFound = false;
using (var diffImage = new MagickImage() { ColorFuzz = new Percentage(10) })
{
differencesFound = mi1.Compare(mi2, new CompareSettings() { Metric = ErrorMetric.Absolute }, diffImage) > 0;
diffImage.Write(outputPicturePath, format: MagickFormat.Png); // Save the diff image to disk
}
if (differencesFound)
{
Browser.WebDriver.Navigate().GoToUrl(outputPicturePath);
Log.Error("Differences found");
}
else
{
Log.Information($"Files are identical");
}
File.Delete(outputPicturePath);
file1.Delete();
}
}
Example of the test:
[TestDescription("\r\nThis test runs varios reports and compares them with the baseline pre-saved files")]
internalclass TestPDFReports : CheckExt
{
privatestring description;
publicoverridevoidExecute()
{
description = "[Test 1]\r\n";
description += "Run Shipment Packing Slip SO642003, compare with saved baseline" + "\r\n";
using (TestExecution.CreateTestCaseGroup(description))
{
string reportID = "SO642003";
string shipmentNbr = "689455";
//Specify path to the file saved in HSAutomatedTests project folder, that will be used as a baseline to compare generated report tostring baselineFileName = @"ReportBaseLines\SO642003_689455.pdf";
RunReport report = new RunReport(reportID);
report.OpenScreen();
report.ReportParameters.DynamicControl<Selector>("Shipment Nbr.").Select(shipmentNbr);
(FileInfo file1, FileInfo file2) = RunReport.RunReportAndExportPDF(report, baselineFileName);
RunReport.RunPDFCompare(file1, file2);
}
description = "[Test 2]\r\n";
description += "Run Shipment Packing Slip SO642003, compare with saved baseline" + "\r\n";
using (TestExecution.CreateTestCaseGroup(description))
{
string reportID = "SO642003";
string shipmentNbr = "689455";
//Specify path to the file saved in HSAutomatedTests project folder, that will be used as a baseline to compare generated report tostring baselineFileName = @"ReportBaseLines\SO642003_689445.pdf";
RunReport report = new RunReport(reportID);
report.OpenScreen();
report.ReportParameters.DynamicControl<Selector>("Shipment Nbr.").Select(shipmentNbr);
(FileInfo file1, FileInfo file2) = RunReport.RunReportAndExportPDF(report, baselineFileName);
RunReport.RunPDFCompare(file1, file2);
}
}
}
Screenshot showing how baseline files are configured in the solution
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.