How to Download & Validate Excel File in Playwright?

Download excel file and validate in playwright

In most of the modern web application we will have the export data option to different formats such as excel, pdf, doc etc.

When it comes to an automation testing checking the scenario related export also saves good amount of manual time.

Let’s begin with how to export data into Excel file and validate in playwright automation testing.

Step 1 : Firstly you need to install “xlsx” plugin using below command

npm install xlsx

Step 2: Create spec file & Import xlsx in the top of the file

const xlsx = require('xlsx');

Step 3 : Before Clicking on download element or button, you need to accept the popup using below statement

const downloadPromise = page.waitForEvent('download');

Step 4 : Clicking on download element or button

page.getByRole('button',{name: 'download' });

Step 5 : Specify downloading wait time roughly & wait download promise

await page.waitForTimeOut(20000); // 20 seconds to download file
const download = await downloadPromise;

Step 6 : Now let’s get the file location where file downloaded

const filePath = await download.path();

Step 7 : Once we have file location & Sheet name then it is very easy to read the content and validate it

const workbook = xlsx.readFile(filePath);
const worksheet = workbook.Sheets['Sheet1'];
const data = xlsx.utils.sheet_to_json(worksheet);

Step 8 : data object will have the content from the excel file, we can validate using assertion statements

Example : data[row].column

 expect(data[0].Skill1).toEqual('Playwright');
expect(data[0].Skill2).toEqual('Cypress');

Like this you can validate any row and column values.

I HOPE IT WAS HELPFUL…!!! Keep visiting…Keep Learning!!!

Reference: https://playwright.dev/docs

Subscribe & get latest software testing updates: https://www.youtube.com/@testerstalk?sub_confirmation=1

Learn more about Playwright:

> Playwright Automation Full Course

> Playwright API Testing Crash Course

> Playwright with Azure DevOps Complete guide

> Testing Microsoft CRM application using Playwright

=== Playwright GitHub Repositories ===

-> Playwright Automation Full Course https://github.com/BakkappaN/PlaywrightTutorialFullCourse

-> Playwright API Testing Crash Course https://github.com/BakkappaN/PlaywrightAPITestingTutorial

-> Playwright with Azure DevOps Pipeline https://github.com/BakkappaN/PlaywrightAzureDevopsPipeline

-> Playwright with CRM Application Testing https://github.com/BakkappaN/MicrosoftD365CRMPlaywrightFramework

-> Playwright with JavaScript Framework [UI + API] https://github.com/BakkappaN/PlaywrightBaseAutomationFramework

-> Playwright with TypeScript Framework [UI + API] https://github.com/BakkappaN/Playwright-TypeScript-Framework

#playwright #apitesting #qa #softwaretesting #crm #javascript #typescript #ado #azuredeveops #framework #fullcourse #codegen #test #e2e #testing #automation #testerstalk #bakkappan #git #github #selenium #framework #test #tutorials

Playwright Full Course Playlist:

Connect with me on LinkedIn : www.linkedin.com/in/bakkappa-n

Join to LinkedIn Group : https://www.linkedin.com/groups/10393547/

Subscribe my channel to get daily updates: https://www.youtube.com/@testerstalk?sub_confirmation=1

Learn more How to Download & Validate Excel File in Playwright?

Leave a Reply