puppeteer#PageEventObject TypeScript Examples

The following examples show how to use puppeteer#PageEventObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From utopia with MIT License 7 votes vote down vote up
export function consoleDoneMessage(
  page: puppeteer.Page,
  expectedConsoleMessage: string,
  errorMessage?: string,
): Promise<boolean> {
  let handler: (message: PageEventObject['console']) => void = () => {
    console.info('Should not fire.')
  }
  const consoleDonePromise = new Promise<boolean>((resolve, reject) => {
    handler = (message: PageEventObject['console']) => {
      const messageText = message.text()
      if (
        messageText.includes(expectedConsoleMessage) ||
        (errorMessage != null && messageText.includes(errorMessage))
      ) {
        // the editor will console.info('SCROLL_TEST_FINISHED') when the scrolling test is complete.
        // we wait until we see this console log and then we resolve the Promise
        resolve(true)
      } else {
        console.info(`CONSOLE: ${messageText}`)
      }
    }
    page.on('console', handler)
  })
  return timeLimitPromise(
    consoleDonePromise,
    10 * ONE_MINUTE_IN_MS, // 10 minutes.
    `Missing console message ${expectedConsoleMessage} in test browser.`,
  ).finally(() => {
    // Ensure we remove the handler afterwards.
    page.off('console', handler)
  })
}