question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Parametrization API for Playwright Test

See original GitHub issue

Discussed in https://github.com/microsoft/playwright/discussions/10283

<div type='discussions-op-text'>

Originally posted by andrejs-ps November 12, 2021 Currently, there is no API to parametrize individual tests.

To pass in a single parameter, one would use a for loop, as suggested in the documentation (https://playwright.dev/docs/test-parameterize)

const people = ['Alice', 'Bob'];
for (const name in people) {
  test(`testing with ${name}`, async () => {
    // ...
  });
}

For two parameters, one can iterate over keys and values of a map. This isn’t exactly a clean solution, but it gets harder for 3+ parameters

for (const [a, b, c] of [["a", 1, 2], ["b", 3, 4], ["c", 5, 6]]) {
    test(`testing with ${a}  ${b} ${c}`, async () => {
        console.log(a, b, c);
    });
}

I believe Playwright would benefit from a parametrization API, similar to Java’s TestNG or JUnit. Example from JUnit:

@ParameterizedTest
@CsvSource({"a,1", "b,2", "foo,3"})

Given that Playwright has:

test.use({ viewport: { width: 600, height: 900 } });

Playwright could have as below for a single test or a group of tests

test.parameters("a,1", "b,2", "foo,3"});

</div>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:19
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dgozmancommented, Apr 11, 2022

@xelact No workaround for now. I’d suggest a separate step before npm run test, something like npm run generate-tests.

1reaction
xelactcommented, Apr 10, 2022

One aspect that would super useful is if we could create the test(description, method) based on the result of a test.beforeAll(...) or even better of a globalSetup.ts script.

You can do this with globalSetup.ts - just write some data to a file in the globalSetup, and then read it in your test file.

// example.spec.ts

const testCases = require('./generated-by-global-setup.json');
for (const { title, url } of testCases) {
  test(title, async ({ page }) => {
    await page.goto(url);
    expect(await page.screenshot()).toMatchSnapshot('default.png');
  });
}

After this change #12018 in v1.19, it is no longer possible to dynamically create test cases based on some data written in the globalSetup.ts as the global setup runs after listing the test cases, is there any workaround?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parametrize tests | Playwright
Parameterized Projects​. Playwright Test supports running multiple test projects at the same time. In the following example, we'll run two projects with ...
Read more >
Parameterize Tests and usage of Text locator in Playwright
So this is how we can easily parametrize our tests in playwright. We​​ can simply use​​ the text of the​​ above​​ link and...
Read more >
Using Playwright for API testing - Reflect.run
A walkthrough on how to use Playwright to create standalone API tests as well as incorporate API calls into a larger end-to-end testing...
Read more >
API testing with Playwright & odottaa | by Yevhen Laichenkov
In this article, I'm going to share a quick overview of how easy it can be to quickly set up your project to...
Read more >
Advanced: configuration | Playwright - CukeTest
To set something up once before running all tests, use globalSetup option in the configuration file. Global setup file must export a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found