Skip to contentPlayForge

How do I run the same test with different data in Playwright?

Written by Jatin Thacker, Automation Architect · Updated

Put the cases in an array and loop over it, calling test() once per item with the data in the title. Each item becomes its own test in the report, so one failing case does not hide the others.

This answer is one piece of Chapter 21: Test Data.

  • Data-Driven Testing Patterns
  • Reading and Writing Excel Test Data
  • Many Users and Roles in One Spec
  • Building the Test Layer

Playwright calls this parameterising tests, and its docs show the loop pattern directly. You write the test once, and the loop registers it once per row of data. A comment in the docs' example points out the condition that makes it work: it is fine "as long the test name is unique". Putting the data in the title, here with a template literal, does that for you.

This is better than one test that checks every product in turn. In a single test, the first wrong price stops the run, so you learn about one defect at a time. As separate tests, each case passes or fails on its own, and the report names exactly which product broke.

In the example, three products from the practice app's catalogue each get a test that opens the catalogue and checks that card's price. The run shows three tests with three different titles. The data could just as well come from a JSON file, as long as each row produces a unique title.

param.spec.js(https://practice.learnplayforge.com)
import { test, expect } from '@playwright/test'; const products = [ { sku: 'MRD-1001', name: 'Aurora Pour-Over Kettle', price: '₹4,290' }, { sku: 'MRD-1003', name: 'Halcyon Burr Grinder', price: '₹11,900' }, { sku: 'MRD-1005', name: 'Bellwether Espresso Tamper', price: '₹1,890' }, ]; for (const { sku, name, price } of products) { test(`${name} is priced ${price}`, async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); const card = page.locator(`article[data-sku="${sku}"]`); await expect(card.getByTestId('product-price')).toHaveText(price); }); }
Output
  ok 1 › Aurora Pour-Over Kettle is priced ₹4,290 (1.2s)
  ok 2 › Halcyon Burr Grinder is priced ₹11,900 (1.1s)
  ok 3 › Bellwether Espresso Tamper is priced ₹1,890 (1.2s)

  3 passed (4.6s)
Test yourself
You loop over three products and call test('price check', ...) with the same title each time. What is the problem?

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.