diff --git a/.gitignore b/.gitignore index d9189c99..80bbaf0e 100644 --- a/.gitignore +++ b/.gitignore @@ -98,7 +98,7 @@ playwright/.cache/ # Midscene.js dump files __ai_responses__/ - +midscene_run .nx/cache .nx/workspace-data \ No newline at end of file diff --git a/packages/midscene/src/insight/utils.ts b/packages/midscene/src/insight/utils.ts index 2643b781..7edce177 100644 --- a/packages/midscene/src/insight/utils.ts +++ b/packages/midscene/src/insight/utils.ts @@ -19,6 +19,7 @@ import { getDumpDir, getPkgInfo, insightDumpFileExt, + stringifyDumpData, writeDumpFile, } from '@/utils'; @@ -55,7 +56,7 @@ export function writeInsightDump( logFileName = `${pid}_${baseData.logTime}-${Math.random()}`; } } - const dataString = JSON.stringify(finalData, null, 2); + const dataString = stringifyDumpData(finalData, 2); if (typeof logIdIndexMap[id] === 'number') { logContent[logIdIndexMap[id]] = dataString; diff --git a/packages/midscene/src/utils.ts b/packages/midscene/src/utils.ts index 4343a35c..ec5b4563 100644 --- a/packages/midscene/src/utils.ts +++ b/packages/midscene/src/utils.ts @@ -127,3 +127,17 @@ export async function sleep(ms: number) { } export const commonScreenshotParam = { type: 'jpeg', quality: 75 } as any; + +export function replacerForPageObject(key: string, value: any) { + if (value && value.constructor?.name === 'Page') { + return '[Page object]'; + } + if (value && value.constructor?.name === 'Browser') { + return '[Browser object]'; + } + return value; +} + +export function stringifyDumpData(data: any, indents?: number) { + return JSON.stringify(data, replacerForPageObject, indents); +} diff --git a/packages/web-integration/src/common/agent.ts b/packages/web-integration/src/common/agent.ts index fefa2774..ae6a7837 100644 --- a/packages/web-integration/src/common/agent.ts +++ b/packages/web-integration/src/common/agent.ts @@ -1,6 +1,10 @@ import type { WebPage } from '@/common/page'; import type { ExecutionDump, GroupedActionDump } from '@midscene/core'; -import { groupedActionDumpFileExt, writeDumpFile } from '@midscene/core/utils'; +import { + groupedActionDumpFileExt, + stringifyDumpData, + writeDumpFile, +} from '@midscene/core/utils'; import { PageTaskExecutor } from '../common/tasks'; import type { AiTaskCache } from './task-cache'; @@ -41,7 +45,7 @@ export class PageAgent { this.dumpFile = writeDumpFile({ fileName: `playwright-${this.testId}`, fileExt: groupedActionDumpFileExt, - fileContent: JSON.stringify(this.dumps), + fileContent: stringifyDumpData(this.dumps), }); } diff --git a/packages/web-integration/src/playwright/cache.ts b/packages/web-integration/src/playwright/cache.ts index d27058f5..8cca510b 100644 --- a/packages/web-integration/src/playwright/cache.ts +++ b/packages/web-integration/src/playwright/cache.ts @@ -2,7 +2,11 @@ import fs from 'node:fs'; import path, { join } from 'node:path'; import type { AiTaskCache } from '@/common/task-cache'; import { findNearestPackageJson } from '@/common/utils'; -import { getDumpDirPath, writeDumpFile } from '@midscene/core/utils'; +import { + getDumpDirPath, + stringifyDumpData, + writeDumpFile, +} from '@midscene/core/utils'; export function writeTestCache( taskFile: string, @@ -13,7 +17,7 @@ export function writeTestCache( writeDumpFile({ fileName: `${taskFile}(${taskTitle})`, fileExt: 'json', - fileContent: JSON.stringify( + fileContent: stringifyDumpData( { pkgName: packageJson.name, pkgVersion: packageJson.version, @@ -21,7 +25,6 @@ export function writeTestCache( taskTitle, ...taskCacheJson, }, - null, 2, ), type: 'cache',