← Back to library
Playwrightfixturesintermediatev1.20+

Data-driven tests with a for loop

Generate one named test per data row by looping over an array with test() calls.

1import { test, expect } from '@playwright/test';
2
3const plans = [
4  { name: 'Free',  price: '$0',  maxUsers: '1 user'   },
5  { name: 'Pro',   price: '$12', maxUsers: '5 users'  },
6  { name: 'Team',  price: '$49', maxUsers: '25 users' },
7];
8
9// Each iteration registers a separately named test —
10// report shows: "pricing card: Free", "pricing card: Pro", etc.
11for (const { name, price, maxUsers } of plans) {
12  test(`pricing card: ${name}`, async ({ page }) => {
13    await page.goto('/pricing');
14
15    const card = page.locator('[data-testid="pricing-card"]').filter({ hasText: name });
16    await expect(card.getByTestId('plan-price')).toHaveText(price);
17    await expect(card.getByTestId('plan-users')).toHaveText(maxUsers);
18  });
19}
#data-driven#loop#parametrize#test.each#for