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.

[Question] Loop tests based on API data array

See original GitHub issue

I’m trying to get the details of the test from db via API, but when I try to fetch the data from API endpoint and run the test it fails with no tests found. Please find the code I’m using below.

import fetch from "node-fetch";
import { test } from "../steps/base-test";

test.describe.serial("Test Suite", () => {    
    getSuiteTests("http://localhost:8090/v7.4/tests/101").then(data => {
        for (const suiteTest of JSON.parse(JSON.stringify(data))) {
            console.log("Test: ", suiteTest);
            test(`${suiteTest.TestSequence}_${suiteTest.TestName}`, async ({ page }) => {
                console.log("Inside Test Case", suiteTest.TestDescription);
            });
        }
    });
});

async function getSuiteTests(url: string): Promise<string> {
    const response = await fetch(url);
    let data = await response.json();
    return data;
}

Note: If I wrap getSuitesTest inside test(i.e. opposite to above code), it actually displays the data from the API.

[
	{
		"TestID": 1003,
		"TestSequence": "TC01",
		"TestName": "SubscribeToNewsLetterTest",
		"TestDescription": "To verify that user is able to Subscribe to NewsLetter",
		"RunTest": true
	},
	{
		"TestID": 1004,
		"TestSequence": "TC02",
		"TestName": "UnsubscribeToNewsLetterTest",
		"TestDescription": "To verify that user is able to Unsubscribe from NewsLetter",
		"RunTest": true
	}
]

Please suggest what I’m doing wrong here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
daddymancommented, Feb 14, 2022

The way I have seen this done is to have a separate program, that runs before the tests, read the data and write a JSON file. When the tests run they import the JSON file, loop through the data, and call test as needed to register all of the tests.

0reactions
dgozmancommented, Feb 14, 2022

The approach described by @daddyman is a great one. Alternatively, you can have a single test with many steps, depending on what you find more convenient.

I think it is unlikely that Playwright Test is going to change in this regard, so closing the issue. Thank you for the discussion!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loop request based on data from response in Postman
In this Postman tutorial, I wanted to show you how to reuse a request and loop over a set of data from a...
Read more >
Jest looping through dynamic test cases - Stack Overflow
There's an in-built way to do this: test.each(table)(name, fn, timeout) ... where each inner array in the 2D array is passed as args...
Read more >
Nested DataSource loops - SmartBear Community
Hi, I am new to ReadAPI and API testing, so I'm learning as I go along… I'm using data driven testing and have...
Read more >
Loop request with different data | Postman Answers
This collection shows how you can loop over the same request while changing the parameters using the Collection Runner and the postman.
Read more >
Array.prototype.some() - JavaScript - MDN Web Docs - Mozilla
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true...
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