Rayrun
← Back to Discord Forum

Data-driven test with dynamic data set

Hello,

I would like to retrieve my data set from our back-end before executing my parametrised tests in a for loop but I don't know what is the best place to do that.

1 - beforeAll hook Unfortunately, playwright generate tests from the test.describe / for loop without executing the beforeAll hook. So I get No tests found error because my dataset object is empty.

test.beforeAll(async () => {
  products = await getAllProducts();
})

test.describe.parallel('Add to Cart from Product page', () => {
  for (const category in products) {
    for (const product of products[category]) {

**2 - Fixtures ** I tried with fixtures but it's not possible to share fixtures with test.describe call so I can't loop on it.

test.describe.parallel('Add to Cart from Product page', ({ products }) => {
  for (const category in products) {
    for (const product of products[category]) {

**3 - Setup test ** I saved my retrieved dataset into a json file and import it in my test file, but when I execute my tests the import fail because the file doesn't exist yet.

setup('Get all products', async () => {
  const products = await getAllProducts();

  // Save products as JSON file
  const filePath = 'tests/.datasets/products.json';
  const jsonData = JSON.stringify(products, null, 2);
  fs.writeFileSync(filePath, jsonData);
})

In my test

import products from '../.dataset/products.json'

Did I reach a limit ? I would appreciate any help or example <a:lourdleson:1146390517928689674> Thank you

This thread is trying to answer question "What is the best way to retrieve a data set from a back-end before executing parametrised tests in a for loop using Playwright?"

4 replies

I found this https://github.com/microsoft/playwright/issues/24273 👁️ But that's not helping imo, I can't perform an sync call outside of my test.

I did it like your 3-rd step, however the script that writes the file is just a js file outside of playwright, and it runs before the playwright execution, by doing it like this, when the setup starts you'll have the file available. Then you can do something like: script in package.json:

"test": "node writeMyFile.js && npx playwright test"

Thank you, I'm actually doing it that way with a npm script:

"preplaywright": "node tests/scripts/getAllProducts.mjs",
"playwright": "playwright test",

But I'm actually struggling with commonjs modules vs es6 <:sadpeter:1146389008914579527>

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 [email protected].