Rayrun
← Back to Discord Forum

Parametrized tests

Hello,

I am looking into running Playwright tests with data coming from JSON file in a for loop. I would like to run same tests for different product URLs that have different data which comes from the JSON file.

The problem -> Playwright doesn't recognize the array with data and doesn't run any tests in the for loop.

Any ideas on how can I solve this problem? Thanks!

This thread is trying to answer question "How can I run Playwright tests with data coming from a JSON file in a for loop?"

13 replies

If you could show a snippet of your for loop and the json file structure. Obviously remove any sensitive data, i’m more than happy to help you fix it.

Sure, here is the test code

test.describe(Filter products on plp name, () => { plpURLsTest.forEach((url) => { test(@p0 Filter products on plp ${url}, async ({ page, productListingPage }) => { await test.step(User navigates to the plp page, async () => { await page.goto(url.url); });

});

}); });

and in the before I have

test.beforeEach(async ({ plpURLs }) => {

});

of course I set plpURLs to the variable

and I am passing these URLs via the config

My steps are:

  1. get brand from env vars
  2. get JSON from brand path
  3. get URLs from that JSON
  4. run tests against those URLs

I think it'll be due to the asyncronous behaviour Could you do something similar to this

  • Define you data structure
  • Load JSON data
  • Run tests for each item in the data array

e.g. data.forEach((item: TestData) => { test.describe(Filter products on plp ${item.name}, () => { test.beforeEach(async ({ page }: { page: Page }) => { await page.goto(item.url); });

I am still getting no tests found most probably because I am setting the data in the precondition. If I try to pass static array defined in the test file, it works.. Any other ideas 😛 ?

test.beforeEach(async () => { console.log(before);

const brand = envConfig().BASE_URL.match(/www.(.*?).c/)![1]; const urlsPath = ./util/urls/${brand}/${brand}.${envConfig().LOCALE}.json; plpURLsTest = JSON.parse(fs.readFileSync(urlsPath, 'utf-8'));

console.log(plpURLsTest = ${plpURLsTest}); });

for (let i = 0; i < plpURLsTest.length; i++) { test.describe(Filter products on plp name, () => { test(@p0 Filter products ${i} on plp ${plpURLs[i]}, async ({ page }) => { await test.step(User navigates to the plp page, async () => { await page.goto(plpURLs[i].url); }); }); }); }

this is the code

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.