chrome-example-test.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. console.info('Starting Chrome Headless');
  11. // Disable shm usage to save resources and prevent random memory
  12. // errors seen on CI
  13. const browser = await puppeteer.launch({
  14. args: ['--no-sandbox', '--disable-dev-shm-usage']
  15. });
  16. const page = await browser.newPage();
  17. console.info('Navigating to page:', URL);
  18. await page.goto(URL);
  19. console.info('Waiting for page to load...');
  20. errored = false;
  21. const handleMessage = async msg => {
  22. const text = msg.text();
  23. console.log(`>> ${text}`);
  24. if (msg.type === 'error') {
  25. errored = true;
  26. }
  27. const lower = text.toLowerCase();
  28. if (lower === 'example started!' || lower === 'test complete!') {
  29. await browser.close();
  30. if (errored) {
  31. throw Error('Example test failed!');
  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. 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. throw up;
  53. });
  54. main();