-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathipympl.test.ts
More file actions
123 lines (100 loc) · 3.72 KB
/
ipympl.test.ts
File metadata and controls
123 lines (100 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IJupyterLabPageFixture, test } from '@jupyterlab/galata';
import { expect } from '@playwright/test';
import * as path from 'path';
const klaw = require('klaw-sync');
async function sleep(time: number) {
return await new Promise((resolve) => setTimeout(resolve, time));
}
const filterUpdateNotebooks = (item) => {
const basename = path.basename(item.path);
return basename.includes('_update');
};
const testCellOutputs = async (
page: IJupyterLabPageFixture,
tmpPath: string,
) => {
const paths = klaw(path.resolve(__dirname, './notebooks'), {
filter: (item) => !filterUpdateNotebooks(item),
nodir: true,
});
const notebooks = paths.map((item) => path.basename(item.path));
for (const notebook of notebooks) {
let results = [];
await page.notebook.openByPath(`${tmpPath}/${notebook}`);
await page.notebook.activate(notebook);
let numCellImages = 0;
const getCaptureImageName = (notebook: string, id: number): string => {
return `${notebook}-cell-${id}.png`;
};
await page.notebook.runCellByCell({
onAfterCellRun: async (cellIndex: number) => {
const cell = await page.notebook.getCellOutput(cellIndex);
if (cell) {
await sleep(500);
results.push(await cell.screenshot());
numCellImages++;
}
},
});
await page.notebook.save();
for (let c = 0; c < numCellImages; ++c) {
expect(results[c]).toMatchSnapshot(
getCaptureImageName(notebook, c),
{ threshold: 0.4 },
);
}
await page.notebook.close(true);
}
};
const testUpdates = async (page: IJupyterLabPageFixture, tmpPath: string) => {
const paths = klaw(path.resolve(__dirname, './notebooks'), {
filter: (item) => filterUpdateNotebooks(item),
nodir: true,
});
const notebooks = paths.map((item) => path.basename(item.path));
for (const notebook of notebooks) {
let results = [];
await page.notebook.openByPath(`${tmpPath}/${notebook}`);
await page.notebook.activate(notebook);
const getCaptureImageName = (notebook: string, id: number): string => {
return `${notebook}-cell-${id}.png`;
};
let cellCount = 0;
await page.notebook.runCellByCell({
onAfterCellRun: async (cellIndex: number) => {
// Always get first cell output which must contain the plot
const cell = await page.notebook.getCellOutput(0);
if (cell) {
await sleep(500);
results.push(await cell.screenshot());
cellCount++;
}
},
});
await page.notebook.save();
for (let i = 0; i < cellCount; i++) {
expect(results[i]).toMatchSnapshot(
getCaptureImageName(notebook, i),
{ threshold: 0.4 },
);
}
await page.notebook.close(true);
}
};
test.describe('ipympl Visual Regression', () => {
test.beforeEach(async ({ page, tmpPath }) => {
await page.contents.uploadDirectory(
path.resolve(__dirname, './notebooks'),
tmpPath,
);
await page.filebrowser.openDirectory(tmpPath);
});
test('Check ipympl first renders', async ({ page, tmpPath }) => {
await testCellOutputs(page, tmpPath);
});
test('Check ipympl update', async ({ page, tmpPath }) => {
await testUpdates(page, tmpPath);
});
});