0

I'm testing my react app and I want to check if XHR request from my app is sent. When I use page.waitFor(5000), it will sometimes work but sometimes not. Is there a better way to make the code wait for XHR to be complete or to be resolved?

Akhilesh krishnan
  • 799
  • 2
  • 8
  • 22
Gal Tubi
  • 1
  • 1
  • Why don't you try javascript promises https://javascript.info/promise-basics – Akhilesh krishnan Feb 05 '19 at 09:51
  • Maybe [`page.waitForResponse()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforresponseurlorpredicate-options) can help? – vsemozhebuty Feb 05 '19 at 10:32
  • 1
    If a specific `DOM` change happens after request has been finished you could use `page.waitForSelector('.class-of-element-showed-after-req-complete')` – Bernhard Feb 05 '19 at 10:33

1 Answers1

1

I think that you need to change your strategy. Instead of waiting for X seconds - trigger an event that satisfy your needs.

Because you test your own application I will suggest listening to your network traffic (following this question)

In short:

  • Enable traffic inspection page.setRequestInterceptionEnabled set to true
  • Register an event listener for each request page.on('request'
  • Check (for every request) if it matches your desirable request (using the request.url property)
  • Call your function when the url matches the desirable request
ymz
  • 6,602
  • 1
  • 20
  • 39