Playwright + JS · Lesson 27 of 35

0/35 done

Module 27: Test Data & Parameterization

27. Data-Driven Testing & Parameterized Scenarios

17 minlesson

Try it yourself

Implement generateCartesianProduct:

  1. Accept listA and listB arrays.
  2. Return array of [a, b] pairs for every combination.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Parameterized Testing with JavaScript Arrays

javascript
import { test, expect } from "@playwright/test";

const searchQueries = [
  { term: "Playwright", expectedCount: 10 },
  { term: "JavaScript", expectedCount: 20 },
  { term: "Automation", expectedCount: 15 },
];

for (const { term, expectedCount } of searchQueries) {
  test(`search for "${term}" returns at least ${expectedCount} results`, async ({ page }) => {
    await page.goto("/search");
    await page.getByPlaceholder("Search").fill(term);
    await page.keyboard.press("Enter");
    await expect(page.locator(".search-result")).toHaveCount(expectedCount);
  });
}

Key Takeaways & Walkthrough Summary

Data-driven testing allows you to run the same test scenario across multiple inputs without duplicating code. In this lesson, we will explore static JSON test fixtures, parameterized test loops, and dynamic test data generation. Let's parameterize our tests!

Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

How does the HTML report display parameterized tests?

Each iteration of the loop creates a completely distinct test entry in the HTML report with its own execution time, video, and trace file.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip