Hi all! So I’ve got a couple of places on the website I’m working on where I validate that a file has been downloaded by accessing the xhr and this works really well. I would like to now extend this to getting the contents of the pdf (perhaps saving it into a json file) and asserting that the information is correct. I have an issue: All the examples I’ve seen online look to make sure that the pdf has very basic information like “PDF-1.4” on line 0 and then name is X.
However, I want to be able to assert that the date displayed is the correct date and the To person is correct and that the line description is correct etc (all content of an invoice for example)
So I thought about getting the pdf information from something like pdf-parser and saving it as a json. I have no idea how to do this.
Has anyone implemented this or have seen something they can point me towards?
Thanks in advance
(For anyone wondering how I work with the xhr:
static VerifyInvoiceHasBeenDownloaded() {
cy.intercept("POST", "**/InvoiceDetailsPdf").as("postDownload");
cy.get(".download-pdf-button").click({ multiple: true });
cy.wait("@postDownload");
cy.get("@postDownload").then(xhr => {
const downloadsFolder = Cypress.config('downloadsFolder');
// @ts-ignore
const fileName = xhr.response.headers['content-disposition'].split('filename=')[1];
const downloadedFilename = path.join(downloadsFolder, fileName);
cy.readFile(downloadedFilename, 'binary', { timeout: 15000 })
.should((buffer) => {
expect(buffer.length).to.be.gt(100);
});
cy.log(`**File ${fileName} exists in downloads folder**`);
cy.readFile(downloadedFilename).should((text) => {
const lines = text.split('\n');
expect(lines).to.have.length.gt(2);
expect(lines[0]).to.equal('%PDF-1.4');
console.log(xhr);
// @ts-ignore
expect(xhr.response.statusCode).to.equal(200);
});
});
}
And then I call it like this in my integration file:
IncomeAndExpensesPage.VerifyInvoiceHasBeenDownloaded();