Skip to content

Commit

Permalink
feat: Allow to import customized test file into the screenshots (#415)
Browse files Browse the repository at this point in the history
#### Description
Now we can run screenshot on a test json file. like.
`[
  {
    "name": "health-3857-z5",
    "tileMatrix": "WebMercatorQuad",
    "location": { "lat": -41.8899962, "lng": 174.0492437, "z": 5 },
    "tileSet": "health"
  }
]
`

#### Intention
*Why is this change being made? What implications or other
considerations are there?*



#### Checklist
*If not applicable, provide explanation of why.*
- [ ] Tests updated
- [ ] Docs updated
- [ ] Issue linked in Title
  • Loading branch information
Wentao-Kuang authored Jul 11, 2024
1 parent 1b7fe14 commit e02fc95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/screenshot.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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',
Expand Down Expand Up @@ -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<void> {
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<TestTile[]>(args.test)));
} else {
tests.push(...DefaultTestTiles);
}

const proms = tests.map((test) => {
return Q(async () => {
const page = await ctx.newPage();

Expand Down
21 changes: 20 additions & 1 deletion src/tiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e02fc95

Please sign in to comment.