elad
elad

Reputation: 196

Puppeteer , listen to network response changes

I am using Puppeteer api (https://github.com/GoogleChrome/puppeteer) For automation testing.

I want to listen to all http response, need the url and the response data for each one.

I try to so use the page.on('response') function :

 page.on('response', response => {
      response.text().then( textBody=> {
             const req = response.request();;
             console.log(req.url())
             console.log(textBody)
      });
})

Should warp in 'waitForSelector' function , to have a flag that the data is ready?

I try to do so.

The problem is some time i do not see any console.log and some time i do.

I will glad to know what do i do wrong?

Upvotes: 1

Views: 2013

Answers (1)

Grant Miller
Grant Miller

Reputation: 29017

There is no need to call response.request(), unless you are trying to obtain the URL of the matching request object.

The following solution will work just fine:

page.on('response', response => {
  console.log('Response URL:', response.url());

  response.text().then(data => {
    console.log('Response Text:');
    console.log(data);
  });
});

If you are still having issues, it could be because the associated request has failed.

You can check for this error by listening for the requestfailed event and logging the result:

page.on('requestfailed', request => {
  console.log('Failed Request URL:', request.url());
  console.log('Failed Request Error Message:', request.failure().errorText);
});

Upvotes: 2

Related Questions