Forked from ryanrosello-og/assert PDF contents using Playwright.ts
Created
July 31, 2023 13:22
-
-
Save iqbmo04/cd5747f369a765c33354bc34155c7a71 to your computer and use it in GitHub Desktop.
assert PDF contents using Playwright
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PDFParser from 'pdf2json'; | |
import { test, expect } from '@playwright/test'; | |
test.describe('assert PDF contents using Playwright', () => { | |
let pdfContents: any | |
test.beforeAll(async () => { | |
pdfContents = await getPDFContents('./pdf_sample.pdf') | |
}) | |
test('pdf file should have 6 pages', async () => { | |
expect(pdfContents.Pages.length, 'The pdf should have 6 pages').toEqual(6); | |
}); | |
test('shows the correct meta informaion (keywords)', async () => { | |
expect(pdfContents.Meta.Keywords, 'PDF keyword was incorrect').toEqual('Standard Fees and Charges, 003-750, 3-750'); | |
}); | |
test('contains the correct subheading text', async () => { | |
const rawText = pdfContents.Pages[0].Texts[3].R[0].T | |
expect(decodeURI(rawText), 'The subheading text was incorrect').toEqual('When we may charge fees'); | |
}); | |
}); | |
async function getPDFContents(pdfFilePath: string): Promise<any> { | |
let pdfParser = new PDFParser(); | |
return new Promise((resolve, reject) => { | |
pdfParser.on('pdfParser_dataError', (errData: { parserError: any }) => | |
reject(errData.parserError) | |
); | |
pdfParser.on('pdfParser_dataReady', (pdfData) => { | |
resolve(pdfData); | |
}); | |
pdfParser.loadPDF(pdfFilePath); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment