chrome-example-test.js 1.9 KB

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