notebook-run.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { expect, galata, test } from '@jupyterlab/galata';
  4. import * as path from 'path';
  5. const fileName = 'simple_notebook.ipynb';
  6. test.use({ tmpPath: 'notebook-run-test' });
  7. test.describe.serial('Notebook Run', () => {
  8. test.beforeAll(async ({ baseURL, tmpPath }) => {
  9. const contents = galata.newContentsHelper(baseURL);
  10. await contents.uploadFile(
  11. path.resolve(__dirname, `./notebooks/${fileName}`),
  12. `${tmpPath}/${fileName}`
  13. );
  14. await contents.uploadFile(
  15. path.resolve(__dirname, './notebooks/WidgetArch.png'),
  16. `${tmpPath}/WidgetArch.png`
  17. );
  18. });
  19. test.beforeEach(async ({ page, tmpPath }) => {
  20. await page.filebrowser.openDirectory(tmpPath);
  21. });
  22. test.afterAll(async ({ baseURL, tmpPath }) => {
  23. const contents = galata.newContentsHelper(baseURL);
  24. await contents.deleteDirectory(tmpPath);
  25. });
  26. test('Run Notebook and capture cell outputs', async ({ page, tmpPath }) => {
  27. await page.notebook.openByPath(`${tmpPath}/${fileName}`);
  28. await page.notebook.activate(fileName);
  29. let numNBImages = 0;
  30. const getCaptureImageName = (id: number): string => {
  31. return `notebook-panel-${id}.png`;
  32. };
  33. const captures = new Array<Buffer>();
  34. await page.notebook.runCellByCell({
  35. onBeforeScroll: async () => {
  36. const nbPanel = await page.notebook.getNotebookInPanel();
  37. if (nbPanel) {
  38. captures.push(await nbPanel.screenshot());
  39. numNBImages++;
  40. }
  41. }
  42. });
  43. // Save outputs for the next tests
  44. await page.notebook.save();
  45. const nbPanel = await page.notebook.getNotebookInPanel();
  46. captures.push(await nbPanel.screenshot());
  47. numNBImages++;
  48. for (let c = 0; c < numNBImages; ++c) {
  49. expect(captures[c]).toMatchSnapshot(getCaptureImageName(c));
  50. }
  51. });
  52. test('Check cell output 1', async ({ page, tmpPath }) => {
  53. await page.notebook.openByPath(`${tmpPath}/${fileName}`);
  54. const cellOutput = await page.notebook.getCellTextOutput(5);
  55. expect(parseInt(cellOutput[0])).toBe(4);
  56. });
  57. test('Check cell output 2', async ({ page, tmpPath }) => {
  58. await page.notebook.openByPath(`${tmpPath}/${fileName}`);
  59. const cellOutput = await page.notebook.getCellTextOutput(6);
  60. expect(parseFloat(cellOutput[0])).toBeGreaterThan(1.5);
  61. });
  62. test('Close Notebook', async ({ page, tmpPath }) => {
  63. await page.notebook.openByPath(`${tmpPath}/${fileName}`);
  64. await expect(page.notebook.close(true)).resolves.not.toThrow();
  65. });
  66. });