mirror of
https://github.com/web-infra-dev/midscene.git
synced 2025-07-03 23:19:38 +00:00

* feat(core): support custom midscene_run dir * feat(report): add search functionality to PlaywrightCaseSelector component * refactor(shared): simplify base directory resolution and remove unused environment variable * feat(shared): integrate shared environment variables across multiple packages * refactor(shared): update base directory resolution to use dynamic midscene_run directory * fix(puppeteer): increase screenshot timeout from 3s to 10s for improved reliability
100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
import { existsSync, readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describeUserPage } from '@/index';
|
|
import { vlLocateMode } from '@midscene/shared/env';
|
|
import { base64Encoded, imageInfoOfBase64 } from '@midscene/shared/img';
|
|
|
|
export async function buildContext(targetDir: string): Promise<{
|
|
context: {
|
|
size: {
|
|
width: number;
|
|
height: number;
|
|
};
|
|
content: any;
|
|
tree: any;
|
|
screenshotBase64: string;
|
|
originalScreenshotBase64: string;
|
|
describer: () => Promise<any>;
|
|
};
|
|
snapshotJson: string;
|
|
screenshotBase64: string;
|
|
originalScreenshotBase64: string;
|
|
}> {
|
|
const originalInputImgP = path.join(
|
|
targetDir,
|
|
existsSync(path.join(targetDir, 'input.png')) ? 'input.png' : 'input.jpeg',
|
|
);
|
|
const originalScreenshotBase64 = base64Encoded(originalInputImgP);
|
|
|
|
const resizeOutputImgP = path.join(targetDir, 'output_without_text.png');
|
|
const snapshotJsonPath = path.join(targetDir, 'element-snapshot.json');
|
|
const elementTreeJsonPath = path.join(targetDir, 'element-tree.json');
|
|
|
|
if (!existsSync(snapshotJsonPath)) {
|
|
console.warn(
|
|
'element-snapshot.json not found, will use input.png to generate context.',
|
|
);
|
|
const size = await imageInfoOfBase64(originalScreenshotBase64);
|
|
const baseContext = {
|
|
size,
|
|
content: [],
|
|
tree: {
|
|
node: null,
|
|
children: [],
|
|
},
|
|
screenshotBase64: originalScreenshotBase64,
|
|
originalScreenshotBase64,
|
|
};
|
|
const result = {
|
|
context: {
|
|
...baseContext,
|
|
describer: async () => {
|
|
return describeUserPage(baseContext);
|
|
},
|
|
},
|
|
snapshotJson: '',
|
|
screenshotBase64: originalScreenshotBase64,
|
|
originalScreenshotBase64,
|
|
};
|
|
return result;
|
|
}
|
|
|
|
const snapshotJson = readFileSync(snapshotJsonPath, { encoding: 'utf-8' });
|
|
const elementSnapshot = JSON.parse(snapshotJson);
|
|
const elementTree = JSON.parse(
|
|
readFileSync(elementTreeJsonPath, { encoding: 'utf-8' }),
|
|
);
|
|
const screenshotBase64 = vlLocateMode()
|
|
? originalScreenshotBase64
|
|
: base64Encoded(resizeOutputImgP);
|
|
|
|
const size = await imageInfoOfBase64(screenshotBase64);
|
|
const baseContext = {
|
|
size,
|
|
content: elementSnapshot,
|
|
tree: elementTree,
|
|
screenshotBase64,
|
|
originalScreenshotBase64,
|
|
};
|
|
|
|
return {
|
|
context: {
|
|
...baseContext,
|
|
describer: async () => {
|
|
return describeUserPage(baseContext);
|
|
},
|
|
},
|
|
snapshotJson,
|
|
screenshotBase64,
|
|
originalScreenshotBase64,
|
|
};
|
|
}
|
|
|
|
export async function getContextFromFixture(pageName: string) {
|
|
const targetDir = path.join(
|
|
__dirname,
|
|
`../../evaluation/page-data/${pageName}`,
|
|
);
|
|
return await buildContext(targetDir);
|
|
}
|