| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { IJupyterLabPageFixture, test } from '@jupyterlab/galata'; |
| 5 |
import { expect } from '@playwright/test'; |
| 6 |
import * as path from 'path'; |
| 7 |
const klaw = require('klaw-sync'); |
| 8 |
|
| 9 |
|
| 10 |
const filterUpdateNotebooks = item => { |
| 11 |
const basename = path.basename(item.path); |
| 12 |
return basename.includes('_update'); |
| 13 |
} |
| 14 |
|
| 15 |
const testCellOutputs = async (page: IJupyterLabPageFixture, tmpPath: string, theme: 'JupyterLab Light' | 'JupyterLab Dark') => { |
| 16 |
const paths = klaw(path.resolve(__dirname, './notebooks'), {filter: item => !filterUpdateNotebooks(item), nodir: true}); |
| 17 |
const notebooks = paths.map(item => path.basename(item.path)); |
| 18 |
|
| 19 |
const contextPrefix = theme == 'JupyterLab Light' ? 'light' : 'dark'; |
| 20 |
page.theme.setTheme(theme); |
| 21 |
|
| 22 |
for (const notebook of notebooks) { |
| 23 |
let results = []; |
| 24 |
|
| 25 |
await page.notebook.openByPath(`${tmpPath}/${notebook}`); |
| 26 |
await page.notebook.activate(notebook); |
| 27 |
|
| 28 |
let numCellImages = 0; |
| 29 |
|
| 30 |
const getCaptureImageName = (contextPrefix: string, notebook: string, id: number): string => { |
| 31 |
return `${contextPrefix}-${notebook}-cell-${id}.png`; |
| 32 |
}; |
| 33 |
|
| 34 |
await page.notebook.runCellByCell({ |
| 35 |
onAfterCellRun: async (cellIndex: number) => { |
| 36 |
const cell = await page.notebook.getCellOutput(cellIndex); |
| 37 |
if (cell) { |
| 38 |
results.push(await cell.screenshot()); |
| 39 |
numCellImages++; |
| 40 |
} |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
await page.notebook.save(); |
| 45 |
|
| 46 |
for (let c = 0; c < numCellImages; ++c) { |
| 47 |
expect(results[c]).toMatchSnapshot(getCaptureImageName(contextPrefix, notebook, c)); |
| 48 |
} |
| 49 |
|
| 50 |
await page.notebook.close(true); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
const testPlotUpdates = async (page: IJupyterLabPageFixture, tmpPath: string, theme: 'JupyterLab Light' | 'JupyterLab Dark') => { |
| 55 |
const paths = klaw(path.resolve(__dirname, './notebooks'), {filter: item => filterUpdateNotebooks(item), nodir: true}); |
| 56 |
const notebooks = paths.map(item => path.basename(item.path)); |
| 57 |
|
| 58 |
const contextPrefix = theme == 'JupyterLab Light' ? 'light' : 'dark'; |
| 59 |
page.theme.setTheme(theme); |
| 60 |
|
| 61 |
for (const notebook of notebooks) { |
| 62 |
let results = []; |
| 63 |
|
| 64 |
await page.notebook.openByPath(`${tmpPath}/${notebook}`); |
| 65 |
await page.notebook.activate(notebook); |
| 66 |
|
| 67 |
const getCaptureImageName = (contextPrefix: string, notebook: string, id: number): string => { |
| 68 |
return `${contextPrefix}-${notebook}-cell-${id}.png`; |
| 69 |
}; |
| 70 |
|
| 71 |
let cellCount = 0; |
| 72 |
await page.notebook.runCellByCell({ |
| 73 |
onAfterCellRun: async (cellIndex: number) => { |
| 74 |
// Always get first cell output which must contain the plot |
| 75 |
const cell = await page.notebook.getCellOutput(0); |
| 76 |
if (cell) { |
| 77 |
results.push(await cell.screenshot()); |
| 78 |
cellCount++; |
| 79 |
} |
| 80 |
} |
| 81 |
}); |
| 82 |
|
| 83 |
await page.notebook.save(); |
| 84 |
|
| 85 |
for (let i = 0; i < cellCount; i++) { |
| 86 |
expect(results[i]).toMatchSnapshot(getCaptureImageName(contextPrefix, notebook, i)); |
| 87 |
} |
| 88 |
|
| 89 |
await page.notebook.close(true); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
test.describe('bqplot Visual Regression', () => { |
| 94 |
test.beforeEach(async ({ page, tmpPath }) => { |
| 95 |
await page.contents.uploadDirectory( |
| 96 |
path.resolve(__dirname, './notebooks'), |
| 97 |
tmpPath |
| 98 |
); |
| 99 |
await page.filebrowser.openDirectory(tmpPath); |
| 100 |
}); |
| 101 |
|
| 102 |
test('Light theme: Check bqplot first renders', async ({ |
| 103 |
page, |
| 104 |
tmpPath, |
| 105 |
}) => { |
| 106 |
await testCellOutputs(page, tmpPath, 'JupyterLab Light'); |
| 107 |
}); |
| 108 |
|
| 109 |
test('Dark theme: Check bqplot first renders', async ({ |
| 110 |
page, |
| 111 |
tmpPath, |
| 112 |
}) => { |
| 113 |
await testCellOutputs(page, tmpPath, 'JupyterLab Dark'); |
| 114 |
}); |
| 115 |
|
| 116 |
test('Light theme: Check bqplot update plot properties', async ({ |
| 117 |
page, |
| 118 |
tmpPath, |
| 119 |
}) => { |
| 120 |
page.on('console', msg => console.log('PAGE LOG:', msg.type, msg.text(), msg.location())); |
| 121 |
page.on("pageerror", err => console.log("PAGE ERROR:", err.message)) |
| 122 |
await testPlotUpdates(page, tmpPath, 'JupyterLab Light'); |
| 123 |
}); |
| 124 |
|
| 125 |
test('Dark theme: Check bqplot update plot properties', async ({ |
| 126 |
page, |
| 127 |
tmpPath, |
| 128 |
}) => { |
| 129 |
page.on('console', msg => console.log('PAGE LOG:', msg.type, msg.text(), msg.location())); |
| 130 |
page.on("pageerror", err => console.log("PAGE ERROR:", err.message)) |
| 131 |
await testPlotUpdates(page, tmpPath, 'JupyterLab Dark'); |
| 132 |
}); |
| 133 |
}); |