chrome-example-test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. console.log(`>> ${text}`);
  20. if (msg.type() === 'error') {
  21. errored = true;
  22. }
  23. const lower = text.toLowerCase();
  24. if (lower === 'example started!' || lower === 'test complete!') {
  25. await browser.close();
  26. if (errored) {
  27. console.error('\n\n***\nExample test failed!\n***\n\n');
  28. process.exit(1);
  29. }
  30. console.info('Example test complete!');
  31. return;
  32. }
  33. };
  34. function handleError(err) {
  35. console.error(err);
  36. errored = true;
  37. }
  38. page.on('console', handleMessage);
  39. page.on('error', handleError);
  40. console.info('Navigating to page:', URL);
  41. await page.goto(URL);
  42. console.info('Waiting for page to load...');
  43. // Wait for the local file to redirect on notebook >= 6.0. Refs:
  44. // https://jupyter-notebook.readthedocs.io/en/stable/changelog.html?highlight=redirect
  45. // https://stackoverflow.com/q/46948489/425458
  46. await page.waitForNavigation();
  47. const html = await page.content();
  48. if (inspect(html).indexOf('jupyter-config-data') === -1) {
  49. console.error('Error loading JupyterLab page:');
  50. console.error(html);
  51. }
  52. console.info('Page loaded');
  53. }
  54. // Stop the process if an error is raised in the async function.
  55. process.on('unhandledRejection', up => {
  56. if (String(up).indexOf('Target closed') === -1) {
  57. throw up;
  58. }
  59. });
  60. main();