[Question] Loop tests based on API data array
See original GitHub issueI’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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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!