The Playwright documentation has a parameterize sample like this. It works:
const people = ['Alice', 'Bob'];
for (const name of people) {
test(`testing with ${name}`, async () => {
// ...
});
// You can also do it with test.describe() or with multiple tests as long the test name is unique.
}
However, I want to get data array before each test,and generate test loop
//data like data[] = [{name:'Alice',age:'20'},..]
let data: any[] = [];
test.beforeEach(async ({ request }) => {
data = await getData(request);
});
for (let i = 0; i < data.length; i++) {
test(`testing with ${name}`, async (page) => {
// ...
});
}
In visual studio code, It shows the error: No Test found
But it works when I assign a exact value let data: any[] =[{name:'Alice',age:'20'}];.
I test similar case https://github.com/microsoft/playwright/issues/9916 How do I properly run individual Playwright tests from a dynamic array of built urls?

The fundamental problem is that Playwright runs the test code twice. The first pass is synchronous, presumably used to detect the number of
test()calls. The second pass actually executes the tests. Logging something in the top-level of the test file shows this two-step process.Unfortunately, at the current time, there's no obvious way to use async code at the top level during the initial test detection step.
This works:
but this doesn't detect tests:
Furthermore,
beforehooks aren't involved as part of the test discovery process. The following codeoutputs:
According to this comment, one workaround is to use
test.step():The problem is the output is missing the test case names:
Another workaround is to generate the data in a separate script before running the tests, store it in a file and read the file synchronously at the start of the tests. For example:
generate-people-data.js:
people.test.js:
Run:
If you don't like the
&&, you can runnpx playwright testas the final step of the generator script.Another option is the synchronous fetch approach from the other thread.
Perhaps there's a more elegant approach in the current version, or future versions of Playwright will support async test detection.