chrome-example-test.js 2.0 KB

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