chrome-example-test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const html = await page.content();
  44. if (inspect(html).indexOf('jupyter-config-data') === -1) {
  45. console.error('Error loading JupyterLab page:');
  46. console.error(html);
  47. }
  48. console.info('Page loaded');
  49. }
  50. // Stop the process if an error is raised in the async function.
  51. process.on('unhandledRejection', up => {
  52. if (String(up).indexOf('Target closed') === -1) {
  53. throw up;
  54. }
  55. });
  56. main();