From ec929dfda4eef827ffbee51078ecae5018cb7789 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Fri, 12 Jul 2024 11:12:23 +1200 Subject: [PATCH] Allow to import customized test file into the screenshots --- src/screenshot.ts | 21 +++++++++++++++++---- src/tiles.ts | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/screenshot.ts b/src/screenshot.ts index ab5ae75..939dc14 100644 --- a/src/screenshot.ts +++ b/src/screenshot.ts @@ -1,9 +1,9 @@ import { fsa } from '@chunkd/fs'; -import { command, number, option, string } from 'cmd-ts'; +import { command, number, option, optional, string } from 'cmd-ts'; import { mkdir } from 'fs/promises'; import { chromium, Browser } from 'playwright'; import { logger } from './log.js'; -import { DefaultTestTiles } from './tiles.js'; +import { DefaultTestTiles, TestTile } from './tiles.js'; import pLimit from 'p-limit'; export const CommandScreenshot = command({ @@ -13,6 +13,11 @@ export const CommandScreenshot = command({ args: { url: option({ type: string, long: 'url', description: 'Basemaps Base URL' }), output: option({ type: string, long: 'output', description: 'Output location for screenshots' }), + test: option({ + type: optional(string), + long: 'test', + description: 'Path of the json test file to replace default test', + }), concurrency: option({ type: number, long: 'concurrency', @@ -42,14 +47,22 @@ export const CommandScreenshot = command({ async function takeScreenshots( chrome: Browser, - args: { output: string; url: string; concurrency: number; timeout: number }, + args: { output: string; url: string; concurrency: number; timeout: number; test?: string }, ): Promise { const ctx = await chrome.newContext({ viewport: { width: 1280, height: 720 } }); const Q = pLimit(Math.floor(args.concurrency)); // await ctx.tracing.start({ screenshots: true, snapshots: true }); - const proms = DefaultTestTiles.map((test) => { + const tests = []; + + if (args.test) { + tests.push(...(await fsa.readJson(args.test))); + } else { + tests.push(...DefaultTestTiles); + } + + const proms = tests.map((test) => { return Q(async () => { const page = await ctx.newPage(); diff --git a/src/tiles.ts b/src/tiles.ts index 6562061..27a4e0d 100644 --- a/src/tiles.ts +++ b/src/tiles.ts @@ -2,7 +2,26 @@ enum TileMatrixIdentifier { Nztm2000Quad = 'NZTM2000Quad', Google = 'WebMercatorQuad', } -export const DefaultTestTiles = [ + +interface Location { + lat: number; + lng: number; + z: number; + b?: number; + p?: number; +} + +export interface TestTile { + name: string; + tileMatrix: TileMatrixIdentifier; + location: Location; + tileSet: string; + style?: string; + terrain?: string; + hillshade?: string; +} + +export const DefaultTestTiles: TestTile[] = [ { name: 'health-3857-z5', tileMatrix: TileMatrixIdentifier.Google,