Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanrosello-og/a9ba0ae2bdaad17e79378a82ce50745a to your computer and use it in GitHub Desktop.
Save ryanrosello-og/a9ba0ae2bdaad17e79378a82ce50745a to your computer and use it in GitHub Desktop.
assert PDF contents using Playwright
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