Playwright Test makes it easy to implement this pattern using JavaScript or TypeScript.
Using Inline Data
import { test, expect } from '@playwright/test';
const testData = [
{ username: 'user1', password: 'pass1' },
{ username: 'user2', password: 'pass2' },
];
test.describe('Login Tests - Data Driven', () => {
testData.forEach(({ username, password }) => {
test(`Login test for ${username}`, async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#username', username);
await page.fill('#password', password);
await page.click('button[type="submit"]');
await expect(page).toHaveURL(/dashboard/);
});
});
});
Using External JSON File
Create a file named loginData.json:
[
{ "username": "user1", "password": "pass1" },
{ "username": "user2", "password": "pass2" }
]
Then use the following test file:
import { test, expect } from '@playwright/test';
import testData from './loginData.json';
test.describe('Login Tests - JSON Data', () => {
testData.forEach(({ username, password }) => {
test(`Login test for ${username}`, async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#username', username);
await page.fill('#password', password);
await page.click('button[type="submit"]');
await expect(page).toHaveURL(/dashboard/);
});
});
});
Enhancements
CI Integration
Add Playwright tests to GitHub Actions, Azure DevOps, or Jenkins.
- name: Run Playwright Tests
run: npx playwright test
Reporting
Use built-in HTML reporter:
npx playwright show-report
Or integrate with Allure for advanced reporting:
npm install -D allure-playwright