Rayrun
← Back to Discord Forum

PDF file Visual Comparison

nischita0722posted in #help-playwright
Open in Discord
nischita0722

Hi Folks, I'm new to playwright and here seeking help.

I'm trying to automate a scenario where I need to download PDF file which contains Image, Text and QR code and verify the downloaded PDF matches the expected PDF file.

I have no idea how to implement this. Can anyone help me with reference code, please? I'm using typescript language.

This thread is trying to answer question "How can I automate the process of downloading and verifying a PDF file using Playwright and TypeScript?"

1 reply

Answer

Automating PDF Download and Verification with Playwright and TypeScript

First, you need to install Playwright in your project. You can do this by running npm install playwright in your terminal.

npm install playwright

Next, import the necessary modules from Playwright in your TypeScript file:

import { chromium } from 'playwright';

Now, let's create an async function to handle the automation scenario:

async function automateScenario() {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://ray.run');

  const downloadPromise = page.waitForEvent('download');
  await page.click('#download-button');
  const download = await downloadPromise;
  const filePath = await download.path();

  // Verify if downloaded PDF matches with expected PDF
  // Write your code here to compare and verify if 
  // `filePath` matches with expected PDF.

  console.log('PDF verification completed.');
  await browser.close();
}

automateScenario();

In this code, we're using Playwright's waitForEvent method to wait for the 'download' event before triggering the download action. This ensures that we capture the download process and can perform further actions on it.

After triggering the download action, we await on downloadPromise to wait for the download process to complete. We then obtain the path of downloaded file using download.path().

To verify if the downloaded PDF matches with the expected PDF, you can use a library or tool of your choice (e.g., pdf-lib) to compare both files. Implement your comparison logic within the provided comment block in the code.

Finally, we close the browser and cleanup resources using browser.close(). This is a high-level overview of how you can automate such a scenario using Playwright and TypeScript. You may need to adapt and modify this code based on your specific requirements and website structure.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.