parameters - How can I parameterize tests in Playwright to run only specific tests for different environments? - Stack Overflow
[Feature] Simple and elegant data driven parameterized tests
How to use parameterize tests at runtime?
[Feature] Parametrization API for Playwright Test
Videos
Hey everyone,
I'm using Playwright for a little while now, but it's my first time dealing with parameterize tests. My problem is the following, it works well when the arrays are initialised before running the tests (put simply, when the array value is hard coded). But when I want to set the array value at runtime (during test execution) I get errors like:
Error: page.goto: Target page, context or browser has been closed Error: page.goto: net::ERR_ABORTED; maybe frame was detached?
This is a snippet of what my current code is looking:
import { expect, test, type Locator } from '@playwright/test';
const apiVesions = ['1', '2'];
const testItems = new Map<string, Locator>();
for (const apiVersion of apiVesions) {
test.describe('Test Suite', async () => {
test(`v${apiVersion}`, async ({ page }) => {
testItems.clear();
await page.goto('/');
const buttons = await page.locator('li').getByRole('button').all();
for (const button of buttons) {
const title = await button.inputValue();
testItems.set(title, button);
}
for (const [title, button] of testItems) {
test.step(title, async () => {
// add a random assertion here
await expect(button).toHaveText(title);
});
}
});
});
}I put console.log everywhere, but the last loop (set dynamically), is never reach/executed. But loop correctly the first `apiVersions` array (in the real code, I have 3 hard coded arrays, no issue).
So, my question is, is it possible to achieve this, or does Playwright want some deterministic thing?
And, if possible, ideally, I want the loop to be for a `test` and not `test.step` (but currently tried, not working too)
And, lastly I'm taking any idea/resources, to try, even the wildest ones (but don't tell me to use another framework 😂 I can't, it's a job constrain).
Thanks for taking the time to read my post!
EDIT
My goal is not ONLY to loop through a list of Locator and perform some action. I need to perform the same series of task on a list (that change depending on version, environment, and a cluster location — those arrays are already hardcoded). Now, when everything is set up, the actual list is grabbed during test execution. I want to use `test` annotation (not `test.step` actually) to generate a good report. Another reason is that, a task may fail on one item on the list and should not impact the other ones (this is why `test.step` is a bit restrictive, even if I can wrap the task around a `try-catch` block to avoid the test failing). I attached a screenshot of the failing run to give you an idea.
PS: `auto()` is not a Playwright function or whatever, if you are interested you can look at it here: https://github.com/lucgagan/auto-playwright (DISCLAIMER: not mine)
EDIT 2 (not encouraging)
When I don't initialise the dynamic array (with an empty array []) the test don't actually start 🙁. Note that it's not even the first loop in the test, I perform some action before reaching it. This is the error:
TypeError: myArray is not iterable
EDIT 3 (good news)
Found this: https://github.com/microsoft/playwright/issues/9916
And, finally make `test.step` working. I forgot to await the `test.step` (my bad 🙁). But, I'm still open, if there is a way to use `test` directly.