@weapp-vite/miniprogram-automator 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -232,6 +232,7 @@ declare class Page {
232
232
  //#region src/MiniProgram.d.ts
233
233
  interface IScreenshotOptions {
234
234
  path?: string;
235
+ timeout?: number;
235
236
  }
236
237
  interface IAuditsOptions {
237
238
  path?: string;
@@ -281,7 +282,7 @@ declare class MiniProgram extends EventEmitter {
281
282
  * @description 等待小程序 App 域协议可用,避免返回半就绪自动化会话。
282
283
  */
283
284
  waitForAppReady(timeout?: number): Promise<void>;
284
- screenshot(options?: IScreenshotOptions): Promise<any>;
285
+ screenshot(options?: IScreenshotOptions): Promise<string | undefined>;
285
286
  testAccounts(): Promise<any>;
286
287
  /**
287
288
  * @description 获取开发者工具 Tool 域基础信息。
@@ -413,7 +414,7 @@ declare class SmartappApp {
413
414
  retry?: number;
414
415
  timeout?: number;
415
416
  }): Promise<SmartappPage>;
416
- screenshot(options?: ScreenshotOptions): Promise<any>;
417
+ screenshot(options?: ScreenshotOptions): Promise<string | undefined>;
417
418
  currentPage(): Promise<SmartappPage>;
418
419
  data(): Promise<any>;
419
420
  tabs(): Promise<never[]>;
package/dist/index.mjs CHANGED
@@ -683,7 +683,7 @@ async function acquireAutomatorPortLease(preferredPort) {
683
683
  }
684
684
  //#endregion
685
685
  //#region package.json
686
- var version = "1.2.3";
686
+ var version = "1.2.4";
687
687
  //#endregion
688
688
  //#region src/Native.ts
689
689
  /** Native 的实现。 */
@@ -885,6 +885,8 @@ const CHANGE_ROUTE_POLL_DELAY = 500;
885
885
  const APP_READY_TIMEOUT = 2e4;
886
886
  const APP_READY_PROBE_TIMEOUT = 3e3;
887
887
  const APP_READY_POLL_DELAY = 500;
888
+ const SCREENSHOT_RETRIES = 2;
889
+ const SCREENSHOT_RETRY_DELAY = 500;
888
890
  const CHANGE_ROUTE_DEBUG_ENABLED = process.env.WEAPP_VITE_E2E_CHANGE_ROUTE_DEBUG === "1";
889
891
  function sleep(ms) {
890
892
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -920,6 +922,11 @@ function isPageStackProtocolTimeout(error) {
920
922
  function isCallWxMethodProtocolTimeout(error) {
921
923
  return error instanceof Error && "code" in error && error.code === "DEVTOOLS_PROTOCOL_TIMEOUT" && "method" in error && error.method === "App.callWxMethod";
922
924
  }
925
+ function isCaptureScreenshotRecoverableError(error) {
926
+ if (!(error instanceof Error)) return false;
927
+ if ("code" in error && error.code === "DEVTOOLS_PROTOCOL_TIMEOUT" && "method" in error && error.method === "App.captureScreenshot") return true;
928
+ return error.message.includes("fail to capture screenshot") || error.message.includes("App.captureScreenshot");
929
+ }
923
930
  function isRouteContextProbeError(error) {
924
931
  return isCurrentPageProtocolTimeout(error) || isPageStackProtocolTimeout(error);
925
932
  }
@@ -1128,9 +1135,20 @@ var MiniProgram = class extends EventEmitter {
1128
1135
  throw lastError instanceof Error ? lastError : /* @__PURE__ */ new Error(`Timed out waiting App domain readiness after ${timeout}ms`);
1129
1136
  }
1130
1137
  async screenshot(options = {}) {
1131
- const { data } = await this.send("App.captureScreenshot");
1132
- if (!options.path) return data;
1133
- await fs.writeFile(options.path, data, "base64");
1138
+ const sendOptions = options.timeout ? { timeout: options.timeout } : void 0;
1139
+ let lastError;
1140
+ let data;
1141
+ for (let attempt = 1; attempt <= SCREENSHOT_RETRIES; attempt += 1) try {
1142
+ data = (await this.send("App.captureScreenshot", {}, sendOptions)).data;
1143
+ if (!options.path) return data;
1144
+ await fs.writeFile(options.path, data, "base64");
1145
+ return;
1146
+ } catch (error) {
1147
+ lastError = error;
1148
+ if (!isCaptureScreenshotRecoverableError(error) || attempt === SCREENSHOT_RETRIES) throw error;
1149
+ await sleep(SCREENSHOT_RETRY_DELAY);
1150
+ }
1151
+ throw lastError;
1134
1152
  }
1135
1153
  async testAccounts() {
1136
1154
  return (await this.send("Tool.getTestAccounts")).accounts;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/miniprogram-automator",
3
3
  "type": "module",
4
- "version": "1.2.3",
4
+ "version": "1.2.4",
5
5
  "description": "完全兼容微信 miniprogram-automator 的现代化替代实现",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",