Skip to main content
Solved

How to use Core.Comparator.Pdf.Compare in TestSDK


Forum|alt.badge.img

Hello,

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):

public class RunReport : Wrappers.ReportLauncher
{
    public RunReport(string screenID)
    {
        base.ScreenId = screenID;
        base.ScreenUrl = $"/frames/reportlauncher.aspx?id={screenID}.rpx";
        ToolBar = new PxToolBar(null);
    }

    public c_parameters ReportParameters => Parameters;

    public static (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);
    }

    public static void RunPDFCompare(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")]
internal class TestPDFReports : CheckExt
{
    private string description;

    public override void Execute()
    {
        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 to
            string 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 to
            string 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

 

Screenshot of the result:

 

View original
Did this topic help you find an answer to your question?

2 replies

Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • 16 replies
  • Answer
  • August 28, 2024

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):

public class RunReport : Wrappers.ReportLauncher
{
    public RunReport(string screenID)
    {
        base.ScreenId = screenID;
        base.ScreenUrl = $"/frames/reportlauncher.aspx?id={screenID}.rpx";
        ToolBar = new PxToolBar(null);
    }

    public c_parameters ReportParameters => Parameters;

    public static (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);
    }

    public static void RunPDFCompare(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")]
internal class TestPDFReports : CheckExt
{
    private string description;

    public override void Execute()
    {
        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 to
            string 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 to
            string 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

 

Screenshot of the result:

 


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 2809 replies
  • August 28, 2024

Thank you for sharing your solution with the community @abaranovhs!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings