From 2a671e036fef95d4de39465d988431db837ab177 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 17 Jun 2024 11:33:36 +0200 Subject: [PATCH] docs(test-parameterize): improve forEach example --- docs/src/test-parameterize-js.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/src/test-parameterize-js.md b/docs/src/test-parameterize-js.md index e6cc791207..99c047e37f 100644 --- a/docs/src/test-parameterize-js.md +++ b/docs/src/test-parameterize-js.md @@ -9,15 +9,21 @@ You can either parameterize tests on a test level or on a project level. ## Parameterized Tests ```js title="example.spec.ts" -const people = ['Alice', 'Bob']; -for (const name of people) { - test(`testing with ${name}`, async () => { - // ... - }); +[ + { name: 'Alice', expected: 'Hello, Alice!' }, + { name: 'Bob', expected: 'Hello, Bob!' }, + { name: 'Charlie', expected: 'Hello, Charlie!' }, +].forEach(({ name, expected }) => { // You can also do it with test.describe() or with multiple tests as long the test name is unique. -} + test(`testing with ${name}`, async ({ page }) => { + await page.goto(`/greet?name=${name}`); + await expect(page.getByRole('heading', { name })).toBeVisible(); + }) +}); ``` +When looping over `before{Each,All}` or `after{Each,All}` hooks, they will be executed for every iteration, so in most cases, you should loop only over tests. + ## Parameterized Projects Playwright Test supports running multiple test projects at the same time. In the following example, we'll run two projects with different options.