notebook-run.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { describe, galata, test } from '@jupyterlab/galata';
  4. import * as path from 'path';
  5. jest.setTimeout(60000);
  6. const fileName = 'simple_notebook.ipynb';
  7. describe('Notebook Run', () => {
  8. beforeAll(async () => {
  9. await galata.resetUI();
  10. galata.context.capturePrefix = 'notebook-run';
  11. });
  12. afterAll(async () => {
  13. galata.context.capturePrefix = '';
  14. });
  15. test('Upload files to JupyterLab', async () => {
  16. await galata.contents.moveFileToServer(
  17. path.resolve(__dirname, `./notebooks/${fileName}`),
  18. `uploaded/${fileName}`
  19. );
  20. await galata.contents.moveFileToServer(
  21. path.resolve(__dirname, './notebooks/WidgetArch.png'),
  22. 'uploaded/WidgetArch.png'
  23. );
  24. expect(
  25. await galata.contents.fileExists(`uploaded/${fileName}`)
  26. ).toBeTruthy();
  27. expect(
  28. await galata.contents.fileExists('uploaded/WidgetArch.png')
  29. ).toBeTruthy();
  30. });
  31. test('Refresh File Browser', async () => {
  32. await expect(galata.filebrowser.refresh()).resolves.toBeUndefined();
  33. });
  34. test('Open directory uploaded', async () => {
  35. await galata.filebrowser.openDirectory('uploaded');
  36. expect(
  37. await galata.filebrowser.isFileListedInBrowser(fileName)
  38. ).toBeTruthy();
  39. });
  40. test('Run Notebook and capture cell outputs', async () => {
  41. await galata.notebook.open(fileName);
  42. expect(await galata.notebook.isOpen(fileName)).toBeTruthy();
  43. await galata.notebook.activate(fileName);
  44. expect(await galata.notebook.isActive(fileName)).toBeTruthy();
  45. let numNBImages = 0;
  46. const getCaptureImageName = (id: number): string => {
  47. return `page-${id}`;
  48. };
  49. await galata.notebook.runCellByCell({
  50. onBeforeScroll: async () => {
  51. const nbPanel = await galata.notebook.getNotebookInPanel();
  52. if (nbPanel) {
  53. if (
  54. await galata.capture.screenshot(
  55. getCaptureImageName(numNBImages),
  56. nbPanel
  57. )
  58. ) {
  59. numNBImages++;
  60. }
  61. }
  62. }
  63. });
  64. const nbPanel = await galata.notebook.getNotebookInPanel();
  65. if (
  66. await galata.capture.screenshot(getCaptureImageName(numNBImages), nbPanel)
  67. ) {
  68. numNBImages++;
  69. }
  70. for (let c = 0; c < numNBImages; ++c) {
  71. expect(
  72. await galata.capture.compareScreenshot(getCaptureImageName(c))
  73. ).toBe('same');
  74. }
  75. });
  76. test('Check cell output 1', async () => {
  77. const cellOutput = await galata.notebook.getCellTextOutput(5);
  78. expect(parseInt(cellOutput[0])).toBe(4);
  79. });
  80. test('Check cell output 2', async () => {
  81. const cellOutput = await galata.notebook.getCellTextOutput(6);
  82. expect(parseFloat(cellOutput[0])).toBeGreaterThan(1.5);
  83. });
  84. test('Close Notebook', async () => {
  85. await expect(galata.notebook.close(true)).resolves.toEqual(true);
  86. });
  87. test('Open home directory', async () => {
  88. await expect(galata.filebrowser.openHomeDirectory()).resolves.toEqual(true);
  89. });
  90. test('Delete uploaded directory', async () => {
  91. await expect(galata.contents.deleteDirectory('uploaded')).resolves.toEqual(
  92. true
  93. );
  94. });
  95. });