Rayrun
โ† Back to Discord Forum

Route validation behavior

Can anyone tell me if there is any way to validate routes regardless of whether they have already been completed or not?

My tests are becoming unstable when using route validation. From what I analyzed and it is very clear, if a route has already been completed, playwright can no longer validate it.

In other words, the route will only be validated if it is being processed or has not yet been started.

Does anyone know if there is a way to validate the route even if it is already finished?

This thread is trying to answer question "Is there a way to validate a route in Playwright even if it has already been completed?"

25 replies

Hi there. Sorry, I can hardly understand your question. Could you please share a code example or describe in more details what do you mean by "route validation"?

Sorry for that @viraxslot .

This is my code: async awaitSuccessfulApiCallClaimID(claimID, method, status = 204) { await this.page.waitForResponse((resp) => resp.url().includes(claimID) && resp.status() == status && resp.request().method() === method); }

And when i run in the pipeline (sometimes locally too) i get this error:

Every time the tests failed, I verified that the route to be valid had been created and executed successfully.

What I have suspected is that playwright is unable to validate a route if it has already been executed, and in these cases, if it is executed faster than validation, the error will occur.

More information: I tried putting the actions + route validation line in a promise, but it doesn't work either

Got it. Possible ways of analyzing this.

  1. If you do the same actions manually will the request be sent in 100% cases? Maybe cache or something.
  2. It's possible that that request already has been sent. You mentioned Promise, but you should try Promise.all and include both "action that triggers request" and your "function that waits for the response".
  3. Try to log all the values from your condition: URL, claimId, status and so on and analyze them.
  4. Record the trace in CI and analyze it locally

Hope it'll help

Thank you for the awsnser @viraxslot !

About the points:

  1. Yes, in 100% of cases the request will be sent.
  2. I mentioned Promise, but I tried it with Promise.all. (sorry).
  3. I've already tried adding logs, and all logs are appearing correctly (request, url, status, id and so on) ๐Ÿ˜ฆ
  4. I did it. hahaha In this case, I check whether the route was sent successfully and with all the value that I am validating.

I really don't know what happens.

With promise.all:

await Promise.all([ list.searchByClaimName(newClaimName), helper.awaitSuccessfulApiCallClaimID(newClaimName, 'GET') ]);

Yeah, it's hard to say without seeing the code. But I got similar problems on my project too and it's 99.99% not playwright problem ๐Ÿ™‚ Is claimID just a string?

It's not related to the problem, but I'd not include "await" into your function name. If you're worried to miss await keyword then configure eslint to check this. As for me await awaitSuccessfulApiCallClaimID() looks really strange.

It is uuid that I get from URL, with this function.

` async getClaimIdFromUrl() { const currentURL = this.page.url(); const match = currentURL.match(//([a-f0-9]+)$/i);

const claimID = match ? match[1] : null;
return claimID;

} `

not a string.

Have you made sure that the waitForResponse promise is done before your navigation, and then the await of the promise is after? EG:

// initialize the promise before the action that triggers the request
const responsePromise = page.waitForResponse(
  (response) =>
    response.url() === "https://example.com" && response.status() === 200
);
const [response] = await Promise.all([
  responsePromise, 
  someButton.click(), //make sure the action is the last promise in the list
]);

Hey @.graff

Yes, I already tried. But I'll do this again to check one more time.

await Promise.all([ helper.awaitSuccessfulApiCallClaimID(claimID, 'DELETE'), list.delete() ]);

I'm not sure if just putting your wrapper function before your action in the promise.All will do it. You could try making a const just before your action and then passing the promise with your action in the promise.All, like this:

const deletePromise = helper.awaitSuccessfulApiCallClaimID(claimID, 'DELETE')
    await Promise.all([
      deletePromise,
      list.delete()
    ]);

Another thing I've found that can be annoying with this pattern is if the action timeout is shorter than the guard wait timeout, you'll get that same error.

just finishing a meet here and I'll try your suggestion!!

Sounds good, hope it works !

You can also try using a promise.AllSettled to find out if all of your calls are rejecting or just the URL event listener.

Hey @.graff
sorry for being late.

I changed my code considering your suggestion, but I still have the same problem ๐Ÿ˜ฆ I ran it locally for a long time and the test passed, but when I ran it in the pipeline, it became unstable.

const awaitRouteDownloadClaimPromise = helper.awaitSuccessfulApiCallClaimID(claimID, 'PATCH');
  await Promise.all([
    awaitRouteDownloadClaimPromise,
    demurrageClaimManager.fillClaimName(downloadClaimName),
  ]);
image.png

Do you see what looks like the intended response in the tracefile output?

I doubt if this is related to missing await. Page closed error usually occurs when you forgot to await somewhere in your code. Just to be safe, try using @typescript-eslint/no-floating-promises ESLint rule to make sure there are no missing awaits before the asynchronous calls to the Playwright API.

Reference: https://playwright.dev/docs/best-practices#lint-your-tests

page.waitForResponse will throw that error if the page (correctly) is torn down after something else times out (like the test timeout in the screenshot) I would recommend either putting a guard assertion in the promise all with a shorter timeout than the waitForResponse

@.graff: Yes, the route with all the information was generated in the pipeline tracefile. ๐Ÿ˜ฆ

Yes, the route with all the information was generated in the pipeline tracefile. ๐Ÿ˜ฆ

@shivaguy: I can do that, but I don't think that's the problem. We can see that my function awaits in this case.

I can do that, but I don't think that's the problem. We can see that my function awaits in this case.

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.