Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Waiting for Several Responses #1732

Open
Mercuron opened this issue Jan 22, 2025 · 1 comment
Open

[Feature]: Waiting for Several Responses #1732

Mercuron opened this issue Jan 22, 2025 · 1 comment

Comments

@Mercuron
Copy link

🚀 Feature Request

There is one case documentated (i use Java):

// Waits for the next response with the specified url
Response response = page.waitForResponse("https://example.com/resource", () -> {
  // Triggers the response
  page.getByText("trigger response").click();
});

But what should i do when i need to wait for several responses, triggered by one action?

Example

I need something like this


ArrayList<Response> responses = page.waitForResponses({ "**/link1","**/link2","**/link3"},()->{
  // Triggers the responses
  page.getByText("trigger 3 responses").click();
});

And using a loop I can check that for example (


        if (response.request().url().contains("link1")){
            assertThat(response.status()).isEqualTo(200);
        }
        if (response.request().url().contains("link2")){
            assertThat(response.status()).isEqualTo(201);
        }
        if (response.request().url().contains("link3")){
            assertThat(response.request().method()).isEqualTo("POST");
            assertThat(response.status()).isEqualTo(204);
        }

I agree, it's not very pretty, but the point is that I can check individually each request for compliance with the parameters.

Motivation

I have many cases when the frontend does not send requests immediately, but after some time. And it seems that the page is already loaded, although any interaction with it provokes an error. Yes, this is rather a question of page loading optimization, the correct order of requests execution, but even such not very well optimized frontend should be tested. Especially when sending requests in several stages (either according to requirements or because it is poorly done - it doesn't matter).
In any case, I need to be able to do the following somehow:

  1. Make an action to trigger multiple requests
  2. Wait for ALL requests to return with certain parameters.
    I have written an example of how I see it, but maybe you can suggest a more convenient way to do it.
@yury-s
Copy link
Member

yury-s commented Jan 23, 2025

You can pass a predicate that would match any of the 3 responses. And if you want to stick with waitForResponse API, you can nest 3 of them with the same predicate. Albeit ugly, it should work:

Response response = page.waitForResponses(predicate, ()->{
   Response response = page.waitForResponses(predicate, ()->{
      Response response = page.waitForResponses(predicate, ()->{
         // Triggers the responses
         page.getByText("trigger 3 responses").click();
      });
      responses.add(response);
   });
   responses.add(response);
});
responses.add(response);

ArrayList<Response> responses = page.waitForResponses({ "**/link1","**/link2","**/link3"},()->{

The problem with such API is it's not quite clear when such method should finish waiting, e.g. some urls may match multiple responses. In such case, you can just use waitForCondition:

page.onResponse(response -> {
   if (matches(response)) {
      responses.add(response);
   }
});
page.waitForCondition(() -> responses.size() == 3);

Would this work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants