title: Puppeteer tests fail when running in headless mode in react application
body: <p>I have a puppeteer test that runs fine when puppeteer has <code>headless: false</code> setting but fails when <code>headless: true</code> is set.</p>
  
  <p>The test steps are:</p>
  
  <ol>
  <li>logs the user in</li>
  <li>takes them to a settings page</li>
  <li>opens a modal</li>
  <li>types in an email into an input element. </li>
  <li>checks that the 'next' button on the page is disabled (the email is not in a correct format). </li>
  </ol>
  
  <p>The test usually runs to the point where it should find the next button. At this point it seems that it has lost focus on the page as it hangs waiting for the next button. I think the type portion causes a react re-render and then the focus on the modal is lost.</p>
  
  <p>Here is the test code:</p>
  
  <pre><code>beforeAll(async () =&gt; {
    await sleep(5000)
    // browser = await puppeteer.launch(getPuppeteerConfig())
    browser_handler = new BrowserHandler
    await wait_for_browser(browser_handler)
  
    const context = await browser_handler.browser.createIncognitoBrowserContext()
    page = await context.newPage()
  
    await page.goto(getGoToPage())
    await page.goto(`${getGoToPage()}/login`)
    console.log('Got to login page')
    await login_customer_email_verified(page)
  }, 150000)
  
  afterEach( async() =&gt; {
    await page.goto(`${getGoToPage()}/dashboard`)
  }, TIMEOUT.SECONDS_7)
  
  //TODO: remove skip after fixing issue of test fails when headless set as 'false'
  describe('Test email validation', () =&gt; {
    test(
      'Should fail email validation for: johndomain.com',
      async () =&gt; {
          await page.waitForNavigation()
          await page.goto(`${getGoToPage()}/settings`)
          console.log('Got to settings page')
  
          await page.waitForSelector('[data-testid="add_new_email_button"]')
          console.log('Found add_new_email_input button')
  
          await page.click('[data-testid="add_new_email_button"]')
  
          await page.waitForSelector('[data-testid="manage_emails_modal"]')
          console.log('Found modal')
  
          await page.waitForSelector('[data-testid="add_new_email_input"]')
          console.log('Found add_new_email_input')
  
          await page.type('[data-testid="add_new_email_input"]', 'johndomain.com')
          console.log('Type successful')
  
          await page.waitForSelector('[data-testid="add_email_next_button"]')
          console.log('Found add_email_next_button')
  
          await page.click('[data-testid=add_email_next_button]') //blur input
          await sleep(TIMEOUT.SECONDS_1)
          await page.click('[data-testid=add_email_next_button]')
  
          await page.waitForSelector('[data-testid="add_new_email_input_error_text"]')
          const error_message = await page.$eval(
            '[data-testid="add_new_email_input_error_text"]',
            text =&gt; text.innerHTML
          )
          expect(error_message).not.toEqual('')
      },
      TIMEOUT.MINUTES_2
    )
  })
  </code></pre>
  
  <p>Console output when running <code>headless: false</code></p>
  
  <p><a href="https://i.stack.imgur.com/U5pwN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U5pwN.png" alt="enter image description here"></a></p>
  
  <p>console output when running <code>headless: true</code></p>
  
  <p><a href="https://i.stack.imgur.com/C1d7f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C1d7f.png" alt="enter image description here"></a></p>
  
  <p>This is the puppeteer config:</p>
  
  <pre><code>export function getPuppeteerConfig(width = 1720, height = 980) {
    let args = [
      '--no-sandbox',
      '--disable-background-timer-throttling',
      '--disable-backgrounding-occluded-windows',
      '--disable-renderer-backgrounding'
    ]
    if (!!width &amp;&amp; !!height) {
      args.push(`--window-size=${width},${height}`)
    }
    return {
      args: args,
      //devtools: true,
      headless: true,
      //slowMo: 25,
      defaultViewport: null,
      pipe: true
    }
  }
  </code></pre>
  
  <p>Can anyone offer any suggestions as to why the puppeteer seems to lose focus part way through? Is there anything I can do to fix it?</p>