To verify the rendering and for example images in a pdf, we recommend using visual verification. You can model the PDF content as a Screen and use Screen Elements like Labels and Labels with Values.
Verificiation by parsing the PDF File
To verify large text content, it is recommended to parse the PDF file into a C# object and verify the content directly in code.
The following chapters guide you through this example.
Access the pdf file from the Execution Host
To enable parsing, first we need to make sure the Execution Host (where the code is actually running) has access directly to the file. TestStep1 contains an example on how to copy the pdf File from the System Under Test to the Execution host, by using a Network Drive. Make sure to setup the Remote Directory Connection String correctly before.
[TestStep(1, TestInput ="Make pdf file accessible for Execution Host")]publicvoidStep1(ITester t){ //the goal is to parse the .pdf file with the used iText7 Nuget //for that the execution host (where to code is running) needs access to the pdf file //the following code copies the file from the System Under Test (SUT) to the Execution host using the RemoteDirectory //Make sure the connection string to the RemoteDirectory is correctly set on the Test Environment //https://docs.testresults.io/designer/interacting-with-the-sut/tabs-overview/environments#remote-directory-connection-stringstring pdfPath =@"C:\\Temp\\";string pdfFileName ="myPdf.pdf";App.SystemHelpers.SetUpRemoteDirectory(true);App.SystemHelpers.CopyFiles(Path.Combine(pdfPath, pdfFileName),App.SystemHelpers.ArtifactsDirectory);var remoteDirectoryOnHost =BaseModelHelpers.MapRemoteDirectoryOnHost(t);if (!remoteDirectoryOnHost.Exists||remoteDirectoryOnHost.GetFiles(pdfFileName).Length==0)thrownewTestStepAbortedException($"Could not read {pdfFileName} on host from remoteDirectory");var _pdfPathOnHost =Path.Combine(remoteDirectoryOnHost.FullName, pdfFileName);}
Parsing the pdf File
In the example project, we use the iText7 Nuget to help parsing the File.
We also implemented a Helper class for the parsing and verification tasks. Add the references Nuget to your Project and copy this Helper class. As iText7 offers a lot of options you can also extend and modify the Helper class according to your needs.
TestStep2 contains the Example on how to verify the pdf Content. In this example you can define which text is expected on each page and line:
[TestStep(2, TestInput ="Verify the content of the pdf", ExpectedResults ="The pdf content is as expected")]publicvoidStep2(ITester t){var helper =newPdfValidationHelper(t);string pdfPath = _pdfPathOnHost ??"myPdf.pdf"; //the variable set in TestStep 1 should be used, but for demo purposes also the pdf project item worksvar expectedPdfContent =newList<ExpectedPDFContent>() { newExpectedPDFContent(page:1, line:13,"Maecenas mauris lectus, lobortis et purus mattis, blandit dictum tellus."),// expected to passnewExpectedPDFContent(page:1, line:13,"Maecenas mauris lectus, lobortis et purus mattis, blandit dictum tellus"),// expected to fail, missing the . at the endnewExpectedPDFContent(page:3, line:20,"In non mauris justo. Duis vehicula mi vel mi pretium, a viverra erat efficitur. Cras aliquam"),// expected to fail, text is differentnewExpectedPDFContent(page:3, line:50,"Lorem ipsum dolor sit amet, consectetur adipiscing elit."),// expected to fail, only 38 lines on page 3newExpectedPDFContent(page:5, line:20,"Lorem ipsum dolor sit amet, consectetur adipiscing elit.") // expected to fail only 4 pages };helper.ValidatePDFAndReport(pdfPath, expectedPdfContent);}