How to download and parse pdf file in cypress test automation

Bothi Palani
1 min readOct 1, 2023

There are scenarios where we need to download the pdf documents in our automation tests and also make sure downloaded file has the expected contents in it.

packages to be installed : pdf-text-reader and chokidar

create a module in support folder, pdfFileReader.ts for waiting for the file to be downloaded and parse the content

import { readPdfText } from 'pdf-text-reader';

import chokidar = require('chokidar');

export const watchPdfFile = () => {
const watcher = chokidar.watch('./cypress/downloads/', {
ignoreInitial: true,
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100,
},
});

return new Promise((resolve, reject) => {
watcher.on('add', file => {
resolve(file);
});
watcher.on('error', error => {
reject(error);
});
});
};

export const readPdf = async () => {
const filePath: any = await watchPdfFile();

return new Promise((resolve, reject) => {
try {
const pages: any = readPdfText(filePath);
resolve(pages);
} catch (e) {
console.log('error reading PDF file');
reject(e);
}
});
};

how to use the readpdf method in the tests,

  downloadAndVerifyPdfContents() {
downloadPdf().click();
cy.task('readPdf').then((pages: any) => {
expect(pages[0]?.lines).to.contain('Campaigns / Campaign Summary');
expect(pages[0]?.lines).to.contain(`Campaign ID ${createCampaign.id}`);
});
}

--

--

Bothi Palani
Bothi Palani

Written by Bothi Palani

Senior SDET & Test Automation Consultant

No responses yet