DEV Community

Cover image for How to Build a Playwright Framework with Excel Data-Driven Testing
Ankit Aloni
Ankit Aloni

Posted on

How to Build a Playwright Framework with Excel Data-Driven Testing

πŸš€ How to Build a Playwright Framework with Excel Data-Driven Testing

Modern test automation requires scalability, reusability, and flexibility.
In this guide, we’ll build a Playwright automation framework that supports:

  • βœ… Data-driven testing using Excel (ExcelJS)
  • βœ… Clean Page Object Model (POM) structure

🧠 Why Data-Driven Testing?

Instead of hardcoding test data, we store it in an Excel file and dynamically run tests with multiple inputs.

πŸ“Œ Use Cases:

  • Login testing with multiple users
  • Form validation
  • Regression testing
  • API + UI test combinations

βš™οΈ Tech Stack

  • Playwright (E2E automation)
  • Node.js
  • ExcelJS (Excel handling)

πŸ“‚ Project Structure

PlayWrightAutomation/
β”‚
β”œβ”€β”€ tests/                # Test cases
β”œβ”€β”€ pages/                # Page Object Models
β”œβ”€β”€ utils/                # Excel utilities
β”œβ”€β”€ test-data/            # Excel test data
β”œβ”€β”€ playwright.config.js
β”œβ”€β”€ package.json
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Step 1: Create Excel Test Data

Example (testData.xlsx):

username password
user1 pass1
user2 pass2

πŸ”§ Step 2: Read Excel Data using ExcelJS

Create utils/excelUtil.js:

const ExcelJS = require("exceljs");

async function getExcelData(sheetName) {
    const workbook = new ExcelJS.Workbook();
    await workbook.xlsx.readFile("./test-data/testData.xlsx");

    const worksheet = workbook.getWorksheet(sheetName);
    const data = [];

    let headers = [];

    worksheet.eachRow((row, rowNumber) => {
        if (rowNumber === 1) {
            headers = row.values;
        } else {
            let rowData = {};
            row.eachCell((cell, colNumber) => {
                rowData[headers[colNumber]] = cell.value;
            });
            data.push(rowData);
        }
    });

    return data;
}

module.exports = { getExcelData };
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 3: Use Data in Playwright Tests

const { test } = require('@playwright/test');
const { getExcelData } = require('../utils/excelUtil');

test.describe("Data Driven Tests", () => {

    let testData;

    test.beforeAll(async () => {
        testData = await getExcelData("Sheet1");
    });

    for (const data of testData) {
        test(`Login Test - ${data.username}`, async ({ page }) => {
            await page.goto("https://example.com");

            console.log("Testing with:", data.username, data.password);
        });
    }
});
Enter fullscreen mode Exit fullscreen mode

⚑ Step 4: Run the Tests

npm install
npx playwright test
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Step 5: View HTML Report

npx playwright show-report
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Report Includes:

  • Passed / Failed tests
  • Execution time
  • Logs

βš™οΈ Advanced Configuration (Coming Soon 🚧)

To make this framework production-ready, upcoming features include:

  • πŸ“± Mobile device emulation (viewport)
  • πŸŽ₯ Screenshots, videos & trace debugging
  • πŸ” SSL handling & environment configs
  • πŸ€– AI-assisted debugging

πŸ‘‰ These will upgrade the framework to a production-grade solution


πŸ”„ CI/CD Integration (Coming Soon 🚧)

  • GitHub Actions setup
  • Automated test execution on push
  • Report publishing

πŸ”₯ Key Benefits

  • Scalable test design
  • Easy test data management
  • Reusable POM structure
  • Real-world automation ready

πŸ”— Full Source Code

πŸ‘‰ https://github.com/ankitaloni369/PlayWrightAutomation


🎯 Conclusion

Combining Playwright with Excel-based data-driven testing enables you to build a powerful, scalable automation framework.


⭐ Support

If you found this useful, consider giving the repo a ⭐ and sharing it!

Top comments (0)