|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import { galata, describe, test } from '@jupyterlab/galata'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as klaw from 'klaw-sync'; |
| 7 | + |
| 8 | +jest.setTimeout(600000); |
| 9 | + |
| 10 | +const filterUpdateNotebooks = item => { |
| 11 | + const basename = path.basename(item.path); |
| 12 | + return basename.includes('_update'); |
| 13 | +} |
| 14 | + |
| 15 | +const testCellOutputs = async (theme: 'JupyterLab Light' | 'JupyterLab Dark') => { |
| 16 | + const paths = klaw('tests/notebooks', {filter: item => !filterUpdateNotebooks(item), nodir: true}); |
| 17 | + const notebooks = paths.map(item => path.basename(item.path)); |
| 18 | + |
| 19 | + let results = []; |
| 20 | + |
| 21 | + const contextPrefix = theme == 'JupyterLab Light' ? 'light_' : 'dark_'; |
| 22 | + galata.theme.setTheme(theme); |
| 23 | + |
| 24 | + for (const notebook of notebooks) { |
| 25 | + galata.context.capturePrefix = contextPrefix + notebook; |
| 26 | + |
| 27 | + await galata.notebook.open(notebook); |
| 28 | + expect(await galata.notebook.isOpen(notebook)).toBeTruthy(); |
| 29 | + await galata.notebook.activate(notebook); |
| 30 | + expect(await galata.notebook.isActive(notebook)).toBeTruthy(); |
| 31 | + |
| 32 | + let numCellImages = 0; |
| 33 | + |
| 34 | + const getCaptureImageName = (id: number): string => { |
| 35 | + return `cell-${id}`; |
| 36 | + }; |
| 37 | + |
| 38 | + await galata.notebook.runCellByCell({ |
| 39 | + onAfterCellRun: async (cellIndex: number) => { |
| 40 | + const cell = await galata.notebook.getCellOutput(cellIndex); |
| 41 | + if (cell) { |
| 42 | + if ( |
| 43 | + await galata.capture.screenshot( |
| 44 | + getCaptureImageName(numCellImages), |
| 45 | + cell |
| 46 | + ) |
| 47 | + ) { |
| 48 | + numCellImages++; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + for (let c = 0; c < numCellImages; ++c) { |
| 55 | + results.push(await galata.capture.compareScreenshot(getCaptureImageName(c))); |
| 56 | + } |
| 57 | + |
| 58 | + await galata.notebook.close(true); |
| 59 | + } |
| 60 | + |
| 61 | + for (const result of results) { |
| 62 | + expect(result).toBe('same'); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const testPlotUpdates = async (theme: 'JupyterLab Light' | 'JupyterLab Dark') => { |
| 67 | + const paths = klaw('tests/notebooks', {filter: item => filterUpdateNotebooks(item), nodir: true}); |
| 68 | + const notebooks = paths.map(item => path.basename(item.path)); |
| 69 | + |
| 70 | + let results = []; |
| 71 | + |
| 72 | + const contextPrefix = theme == 'JupyterLab Light' ? 'light_' : 'dark_'; |
| 73 | + galata.theme.setTheme(theme); |
| 74 | + |
| 75 | + for (const notebook of notebooks) { |
| 76 | + galata.context.capturePrefix = contextPrefix + notebook; |
| 77 | + |
| 78 | + await galata.notebook.open(notebook); |
| 79 | + expect(await galata.notebook.isOpen(notebook)).toBeTruthy(); |
| 80 | + await galata.notebook.activate(notebook); |
| 81 | + expect(await galata.notebook.isActive(notebook)).toBeTruthy(); |
| 82 | + |
| 83 | + let numCellImages = 0; |
| 84 | + |
| 85 | + const getCaptureImageName = (id: number): string => { |
| 86 | + return `cell-${id}`; |
| 87 | + }; |
| 88 | + |
| 89 | + await galata.notebook.runCellByCell({ |
| 90 | + onAfterCellRun: async (cellIndex: number) => { |
| 91 | + // Always get first cell output which must contain the plot |
| 92 | + const cell = await galata.notebook.getCellOutput(0); |
| 93 | + if (cell) { |
| 94 | + if ( |
| 95 | + await galata.capture.screenshot( |
| 96 | + getCaptureImageName(numCellImages), |
| 97 | + cell |
| 98 | + ) |
| 99 | + ) { |
| 100 | + numCellImages++; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + for (let c = 0; c < numCellImages; ++c) { |
| 107 | + results.push(await galata.capture.compareScreenshot(getCaptureImageName(c))); |
| 108 | + } |
| 109 | + |
| 110 | + await galata.notebook.close(true); |
| 111 | + } |
| 112 | + |
| 113 | + for (const result of results) { |
| 114 | + expect(result).toBe('same'); |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +describe('bqplot Visual Regression', () => { |
| 119 | + beforeAll(async () => { |
| 120 | + await galata.resetUI(); |
| 121 | + }); |
| 122 | + |
| 123 | + afterAll(async () => { |
| 124 | + galata.context.capturePrefix = ''; |
| 125 | + }); |
| 126 | + |
| 127 | + test('Upload files to JupyterLab', async () => { |
| 128 | + await galata.contents.moveDirectoryToServer( |
| 129 | + path.resolve(__dirname, `./notebooks`), |
| 130 | + 'uploaded' |
| 131 | + ); |
| 132 | + expect( |
| 133 | + await galata.contents.fileExists('uploaded/scatter.ipynb') |
| 134 | + ).toBeTruthy(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('Refresh File Browser', async () => { |
| 138 | + await galata.filebrowser.refresh(); |
| 139 | + }); |
| 140 | + |
| 141 | + test('Open directory uploaded', async () => { |
| 142 | + await galata.filebrowser.openDirectory('uploaded'); |
| 143 | + expect( |
| 144 | + await galata.filebrowser.isFileListedInBrowser('scatter.ipynb') |
| 145 | + ).toBeTruthy(); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Light theme: Check bqplot first renders', async () => { |
| 149 | + await testCellOutputs('JupyterLab Light'); |
| 150 | + }); |
| 151 | + |
| 152 | + test('Dark theme: Check bqplot first renders', async () => { |
| 153 | + await testCellOutputs('JupyterLab Dark'); |
| 154 | + }); |
| 155 | + |
| 156 | + test('Light theme: Check bqplot update plot properties', async () => { |
| 157 | + await testPlotUpdates('JupyterLab Light'); |
| 158 | + }); |
| 159 | + |
| 160 | + test('Dark theme: Check bqplot update plot properties', async () => { |
| 161 | + await testPlotUpdates('JupyterLab Dark'); |
| 162 | + }); |
| 163 | + |
| 164 | + test('Open home directory', async () => { |
| 165 | + await galata.filebrowser.openHomeDirectory(); |
| 166 | + }); |
| 167 | + |
| 168 | + test('Delete uploaded directory', async () => { |
| 169 | + await galata.contents.deleteDirectory('uploaded'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments