Rayrun
← Back to Discord Forum

Parameterize tests in playwright

Hi. I am trying to parameterize my tests, by first making an api request to azure devops where i am getting my parameters from.

However if i wanted to do the test in iterations i need to wrap my whole test inside a loop that would look something like this:

for(let i = 0; i < parameters.length; i ++) that means i have to do the api call outside my test. But the way playwright acts now is that even though I am trying to await my request response, playwright jumps out of it and starts trying to run the tests: const response = await axios.get(url, { headers }); every time it encounters an await, it passes over it and comes back to it later, but I have to be certain that the response has resolved before i move on. Anybody knows how to get around this?

This thread is trying to answer question "How can I ensure that my API response is resolved before my tests start running in Playwright?"

5 replies

In which context this const response = await axios.get(url, { headers }); happening?

Are your loop and axios call within a test.describe?

No, right now i have a function outside my tests that is called fetchTestCaseParameters(), it's inside here i have my axios call. so it looks a bit like this: par = fetchTestCaseParameters test.beforeeach{ some login methods etc } for-loop { test {my test that i am trying to loop} } it's obviously a bit simplified but thats the gist of it

Why not put your axios call in a beforeAll or beforeEach, if you need those parameters in your tests?

I tried that aswell, but it didnt work. Maybe if i use the method to simply return the URL and headers and then do the call inside beforeall. But when i debugg it seems like playwright doesnt reach my beforeeach and beforeall untill it gets to the playwright test. And when the test is inside the loop, the loop doesnt work because its expecting the request result

Answer

Waiting for API Responses in Playwright

In Playwright, you can use the page.waitForResponse() method to ensure an API response is resolved before proceeding with your tests. This method waits for a specific network response before moving on.

Here's how you can use it:

const responsePromise = page.waitForResponse('https://ray.run/api/endpoint');
await page.click('button'); // This action triggers the API request
const response = await responsePromise; // Wait for the API response

In this example, 'https://ray.run/api/endpoint' is the URL of the API endpoint you're waiting for. After triggering the API request (by clicking a button in this case), you wait for the responsePromise to resolve, which means the API response has been received.

This method ensures your tests only proceed after receiving and handling the necessary data from APIs. It helps synchronize your test flow and ensures accurate testing against expected responses.

For more details on handling network requests in Playwright, check out this blog post.

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.