title: What is the proper way to capture a HTTP response with Puppeteer?
body: <p>I am trying to capture the http response status from a user sign-up. </p>
  
  <p>My code looks like this:</p>
  
  <pre><code>  it.only('returns a 400 response if email is taken', async () =&gt; {
      await page.goto(`${process.env.DOMAIN}/sign-up`)
      await page.waitFor('input[id="Full Name"]')
  
      await page.type('input[id="Full Name"]', 'Luke Skywalker')
      await page.type('input[id="Email"]', 'LukeSkywalker@voyage.com')
      await page.type('input[id="Password"]', 'LukeSkywalker123', {delay: 100})
      await page.click('input[type="submit"]', {delay: 1000})
  
      const response = await page.on('response', response =&gt; response)
  
      console.log('request status', response.status)
      // expect(response).toEqual(400)
    })
  </code></pre>
  
  <p>The docs give an example of intercepting the request and doing things with it:</p>
  
  <pre><code>await page.setRequestInterception(true);
  page.on('request', request =&gt; {
    request.respond({
      status: 404,
      contentType: 'text/plain',
      body: 'Not Found!'
    });
  });
  </code></pre>
  
  <p>And I have tried a similar pattern to no avail, along with many other patterns. Everything I do returns the <code>page</code>, a huge object with no status on it that I can see. Any help is much appreciated. </p>
  
  <p><strong>WHAT WORKED:</strong></p>
  
  <p>thank you to @tomahaug for steering me in the correct direction. My first problem was placement, the listener needs to go be set up before the request is made, I had it just after the request. Makes sense. My biggest issue was assigning the listener to a variable, so that I could call the expect as my last line. Assigning it to a variable caused the <code>page</code> to be returned. What I needed to do was just run the test inside the listener. While using <code>done()</code> throws and error for me I closed off my test as follows below, the working version of my code: </p>
  
  <pre><code>it.only('returns a 400 response if email is taken', async () =&gt; {
      await page.goto(`${process.env.DOMAIN}/sign-up`)
      await page.waitFor('input[id="Full Name"]')
  
      await page.type('input[id="Full Name"]', 'Luke Skywalker')
      await page.type('input[id="Email"]', 'LukeSkywalker@voyage1.com')
      await page.type('input[id="Password"]', 'LukeSkywalker123', {delay: 100})
  
      await page.on('response', response =&gt; {
        if (response.request().method === 'POST' &amp;&amp; response.url === `${process.env.USERS_API_DOMAIN}/sessions`) {
          expect(response.status).toEqual(400)
        }
      })
  
      await page.click('input[type="submit"]', {delay: 1000})
    })
  
    after(async function () {
      await browser.close()
    })
  </code></pre>
  
  <p>Hope this helps someone else!</p>