chrome-example-test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * A puppeteer test that launches an example app and makes sure
  3. * there are no console errors or uncaught errors prior to a sentinal
  4. * string being printed.
  5. */
  6. const puppeteer = require('puppeteer');
  7. const inspect = require('util').inspect;
  8. const URL = process.argv[2];
  9. async function main() {
  10. /* eslint-disable no-console */
  11. console.info('Starting Chrome Headless');
  12. const browser = await puppeteer.launch({
  13. args: ['--no-sandbox']
  14. });
  15. const page = await browser.newPage();
  16. let errored = false;
  17. const handleMessage = async msg => {
  18. const text = msg.text();
  19. if (msg.type() === 'error') {
  20. console.log(`ERROR>> ${text}`);
  21. // console.dir(msg);
  22. errored = true;
  23. } else {
  24. console.log(`>> ${text}`);
  25. }
  26. const lower = text.toLowerCase();
  27. if (lower === 'example started!' || lower === 'test complete!') {
  28. await browser.close();
  29. if (errored) {
  30. console.error('\n\n***\nExample test failed!\n***\n\n');
  31. process.exit(1);
  32. }
  33. console.info('Example test complete!');
  34. return;
  35. }
  36. };
  37. function handleError(err) {
  38. console.error(err);
  39. errored = true;
  40. }
  41. page.on('console', handleMessage);
  42. page.on('error', handleError);
  43. console.info('Navigating to page:', URL);
  44. await page.goto(URL);
  45. console.info('Waiting for page to load...');
  46. // Wait for the local file to redirect on notebook >= 6.0. Refs:
  47. // https://jupyter-notebook.readthedocs.io/en/stable/changelog.html?highlight=redirect
  48. // https://stackoverflow.com/q/46948489/425458
  49. await page.waitForNavigation();
  50. const html = await page.content();
  51. if (inspect(html).indexOf('jupyter-config-data') === -1) {
  52. console.error('Error loading JupyterLab page:');
  53. console.error(html);
  54. }
  55. console.info('Page loaded');
  56. }
  57. // Stop the process if an error is raised in the async function.
  58. process.on('unhandledRejection', up => {
  59. if (String(up).indexOf('Target closed') === -1) {
  60. throw up;
  61. }
  62. });
  63. main();