Puppeteer: Override API Requests and respond with mock data without another package

Viewed 1412

I am trying to get mock data for puppeteer for a dynamic vue application.

Here is what I have (with placeholder API urls):

await page.setRequestInterception(true)
    page.on('request', (request) => {
      console.log(request.url())
      if (request.url() === 'URL HERE') {
        request.respond({
          status: 200,
          content: 'application/json',
          body: JSON.stringify(MockData.image)
        })
      } else if (request.url() === 'URL HERE') {
        request.respond({
          status: 200,
          content: 'application/json',
          body: JSON.stringify(MockData.text)
        })
      } else {
        request.continue()
      }
    })

This snippet sits above all logic where the requests would come through, and the URLS are correct.

The mocked calls do not seem to finish or come through correctly. Is this a problem with my setup?

1 Answers

Solution was to call launch with chromium security disabled.

browser = await puppeteer.launch({
  args: ['--disable-web-security']
})
Related