I implemented PDF content validation in Playwright automation using TypeScript, with the support of AI coding assistants.
How it works:
1. Download the PDF file using Playwright's download event
2. Save the file to a local downloads folder
3. Verify that the file was downloaded successfully
4. Read the PDF file and extract its text content using the pdf-parse library
5. Validate that the expected text exists in the PDF using assertions
// Wait for download event before clicking
const downloadPromise = documentObjectPage.page.waitForEvent('download');
// Click on download button
await documentObjectPage.downloadButton.click();
// Download Promise
const download await downloadPromise;
// save file to known path
const savePath = path.join(downloadsDir, download.suggestedFilename());
await download.saveAs (savePath);
// Store path for use in other methods
downloadedFilePath = savePath;
// Extract text from the downloaded PDF
expect(fs.existssync (downloadedFilePath)).toBeTruthy();
const dataBuffer = fs.readFileSync (downloadedFilePath);
const parser = new PDFParse({ data: dataBuffer });
const pdfData = await parser.getText();
console.log('Extracted PDF text:', pdfData.text);
// verify text content
expect(pdfData.text).toContain (Dummy PDF');
await parser.destroy();
No comments:
Post a Comment