|
1 |
| -import { galata, IJupyterLabPageFixture, test } from "@jupyterlab/galata"; |
2 |
| -import { expect } from "@playwright/test"; |
3 |
| - |
4 |
| -async function renderMap(fileName: string, page: IJupyterLabPageFixture) { |
5 |
| - const fullName = `./${fileName}.ipynb`; |
6 |
| - await page.notebook.openByPath(fullName); |
7 |
| - await page.notebook.activate(fullName); |
8 |
| - await page.notebook.run(); |
9 |
| - await page.notebook.waitForRun(); |
10 |
| - const maps = await page.$("div.leaflet-container"); |
11 |
| - await new Promise((_) => setTimeout(_, 1000)); |
12 |
| - |
13 |
| - // Move the mouse to the center of the map |
14 |
| - const bb = await maps?.boundingBox(); |
15 |
| - await page.mouse.move(bb!.x + bb!.width / 2, bb!.y + bb!.height / 2); |
16 |
| - |
17 |
| - expect(await maps!.screenshot()).toMatchSnapshot({ |
18 |
| - name: `${fileName}.png`, |
19 |
| - }); |
| 1 | +import { IJupyterLabPageFixture, test } from '@jupyterlab/galata'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import * as path from 'path'; |
| 4 | +const klaw = require('klaw-sync'); |
| 5 | + |
| 6 | + |
| 7 | +const filterUpdateNotebooks = item => { |
| 8 | + const basename = path.basename(item.path); |
| 9 | + return basename.includes('_update'); |
20 | 10 | }
|
21 | 11 |
|
22 |
| -const notebookList = [ |
23 |
| - "DivIcon", |
24 |
| - "DrawControl", |
25 |
| - "FullScreenControl", |
26 |
| - "Icon", |
27 |
| - "LayersControl", |
28 |
| - "LegendControl", |
29 |
| - "MagnifyingGlass", |
30 |
| - "Marker", |
31 |
| - "Polyline", |
32 |
| - "Popup", |
33 |
| - "ScaleControl", |
34 |
| - "SplitMapControl", |
35 |
| - "TileLayer", |
36 |
| - "WidgetControl", |
37 |
| - "ZoomControl", |
38 |
| -]; |
39 |
| - |
40 |
| -test.describe("ipyleaflet Visual Regression", () => { |
41 |
| - test.beforeEach(async ({ page }) => { |
42 |
| - page.setViewportSize({ width: 1920, height: 1080 }); |
43 |
| - }); |
44 |
| - for (const name of notebookList) { |
45 |
| - test(`Render ${name}`, async ({ page }) => { |
46 |
| - await renderMap(name, page); |
| 12 | +const testCellOutputs = async (page: IJupyterLabPageFixture, tmpPath: string, theme: 'JupyterLab Light' | 'JupyterLab Dark') => { |
| 13 | + const paths = klaw(path.resolve(__dirname, '../notebooks'), {filter: item => !filterUpdateNotebooks(item), nodir: true}); |
| 14 | + const notebooks = paths.map(item => path.basename(item.path)); |
| 15 | + |
| 16 | + const contextPrefix = theme == 'JupyterLab Light' ? 'light_' : 'dark_'; |
| 17 | + page.theme.setTheme(theme); |
| 18 | + |
| 19 | + for (const notebook of notebooks) { |
| 20 | + let results = []; |
| 21 | + |
| 22 | + await page.notebook.openByPath(`${tmpPath}/${notebook}`); |
| 23 | + await page.notebook.activate(notebook); |
| 24 | + |
| 25 | + let numCellImages = 0; |
| 26 | + |
| 27 | + const getCaptureImageName = (contextPrefix: string, notebook: string, id: number): string => { |
| 28 | + return `${contextPrefix}-${notebook}-cell-${id}.png`; |
| 29 | + }; |
| 30 | + |
| 31 | + await page.notebook.runCellByCell({ |
| 32 | + onAfterCellRun: async (cellIndex: number) => { |
| 33 | + const cell = await page.notebook.getCellOutput(cellIndex); |
| 34 | + if (cell) { |
| 35 | + const map = await cell.$("div.leaflet-container"); |
| 36 | + |
| 37 | + if (map) { |
| 38 | + await new Promise((_) => setTimeout(_, 1000)); |
| 39 | + |
| 40 | + // Move the mouse to the center of the map |
| 41 | + const bb = await map.boundingBox(); |
| 42 | + if (bb) { |
| 43 | + await page.mouse.move(bb.x + bb.width / 2, bb.y + bb.height / 2); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + results.push(await cell.screenshot()); |
| 48 | + numCellImages++; |
| 49 | + } |
| 50 | + } |
47 | 51 | });
|
| 52 | + |
| 53 | + await page.notebook.save(); |
| 54 | + |
| 55 | + for (let c = 0; c < numCellImages; ++c) { |
| 56 | + expect(results[c]).toMatchSnapshot(getCaptureImageName(contextPrefix, notebook, c)); |
| 57 | + } |
| 58 | + |
| 59 | + await page.notebook.close(true); |
48 | 60 | }
|
| 61 | +} |
| 62 | + |
| 63 | +test.describe('ipyleaflet Visual Regression', () => { |
| 64 | + test.beforeEach(async ({ page, tmpPath }) => { |
| 65 | + await page.contents.uploadDirectory( |
| 66 | + path.resolve(__dirname, '../notebooks'), |
| 67 | + tmpPath |
| 68 | + ); |
| 69 | + await page.filebrowser.openDirectory(tmpPath); |
| 70 | + }); |
| 71 | + |
| 72 | + test('Light theme: Check ipyleaflet renders', async ({ |
| 73 | + page, |
| 74 | + tmpPath, |
| 75 | + }) => { |
| 76 | + await testCellOutputs(page, tmpPath, 'JupyterLab Light'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Dark theme: Check ipyleaflet renders', async ({ |
| 80 | + page, |
| 81 | + tmpPath, |
| 82 | + }) => { |
| 83 | + await testCellOutputs(page, tmpPath, 'JupyterLab Dark'); |
| 84 | + }); |
49 | 85 | });
|
0 commit comments