π 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
π 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 };
π§ͺ 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);
});
}
});
β‘ Step 4: Run the Tests
npm install
npx playwright test
π Step 5: View HTML Report
npx playwright show-report
π 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)