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] How to wait for multiple responses ?

See original GitHub issue

In js I can do this


    async waitForFindingUpload(findingName) {
        return await this.page.waitForResponse(async (response) => {
            const body = await response.text();
            return body.includes(findingName)
        });
    }
        const responses = await Promise.all([
            this.waitForFindingUpload("Name of Finding 1"),
            this.waitForFindingUpload("Name of Finding 2"),
            this.#uploadFile()
        ])

How to do the same in java ? Your API suggests this waiting mechanism:

        Response res = page.waitForResponse(response -> {
            return response.url().equals("https://url_/gateway/graphql") &&
                    response.request().postData().contains("sidebar");
        }, () -> {
            page.click("//button[@type='submit']");
        });

this way I can wait only for 1 response

I tried just several waiting in a row after click on a button:

page.click("//button[@type='submit']");
page.waitForResponse()
page.waitForResponse()
page.waitForResponse()
page.waitForResponse()
page.waitForResponse()

and not all of them are resolved. For example after page loaded there are 5 graphql responses, 3-4 consequent waits are resolved I guess based on timings.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
yury-scommented, Apr 27, 2021

You can have nested waitForResponse calls:

    Response response2 = page.waitForResponse(response -> {
      return response.url().contains("first.png");
    }, () -> {
      Response response1 = page.waitForResponse(response -> {
          return response.url().contains("second.html");
        },
        ()-> {
          page.click("//button[@type='submit']");
        });
    });

Or alternatively you can always use page.onResponse() that will be called for all responses.

Hope it answers your question.

0reactions
chaganiucommented, Feb 19, 2022

thanks for this @yury-s . I had to make a small change to the if statement because it was throwing an index out of bounds exception. For anyone else reading this here is how I modified it:

    void checkAllResponses(List<String> urls, Runnable code, Page page) {
        if (urls.isEmpty()) {
            code.run();
        } else {
            String url = urls.remove(urls.size() - 1);
            Response response = page.waitForResponse(url, () -> checkAllResponses(urls, code, page));
            assertTrue(response.ok(), url);
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to wait for multiple fetch responses to be pushed in array ...
You can wait for all the promises with Promise.all . I used map instead of forEach to map all array values to promises....
Read more >
wait for all responses to multiple calls - MuleSoft Help Center
Hello: I am developing a flow in which I have to call n times to the same external system. Each call is synchronous,...
Read more >
How long should we wait after asking a question? - Byrdseed
Hopefully obvious, though: you need to ask interesting, higher-order questions in order to get multiple, thoughtful responses. No matter how long you wait, ......
Read more >
Wait Time: Making Space for Authentic Learning
First, wait time 1 constitutes a 3-5 second pause between asking a question and soliciting an answer. Second, wait time 2 is a...
Read more >
How to Join Multiple Requests in JavaScript with Promise
Why is this helpful? In this case we need to make two requests, wait for them both to be completed, and then pass...
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