@weapp-vite/miniprogram-automator 1.2.0 → 1.2.2
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 +4 -0
- package/dist/index.mjs +51 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -277,6 +277,10 @@ declare class MiniProgram extends EventEmitter {
|
|
|
277
277
|
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
278
278
|
exposeFunction(name: string, bindingFunction: AutomatorCallable): Promise<void>;
|
|
279
279
|
checkVersion(): Promise<void>;
|
|
280
|
+
/**
|
|
281
|
+
* @description 等待小程序 App 域协议可用,避免返回半就绪自动化会话。
|
|
282
|
+
*/
|
|
283
|
+
waitForAppReady(timeout?: number): Promise<void>;
|
|
280
284
|
screenshot(options?: IScreenshotOptions): Promise<any>;
|
|
281
285
|
testAccounts(): Promise<any>;
|
|
282
286
|
/**
|
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.
|
|
686
|
+
var version = "1.2.2";
|
|
687
687
|
//#endregion
|
|
688
688
|
//#region src/Native.ts
|
|
689
689
|
/** Native 的实现。 */
|
|
@@ -880,6 +880,9 @@ const CHANGE_ROUTE_CONTEXT_TIMEOUT = 12e3;
|
|
|
880
880
|
const CHANGE_ROUTE_CALL_TIMEOUT = 12e3;
|
|
881
881
|
const CHANGE_ROUTE_READY_TIMEOUT = 15e3;
|
|
882
882
|
const CHANGE_ROUTE_POLL_DELAY = 500;
|
|
883
|
+
const APP_READY_TIMEOUT = 2e4;
|
|
884
|
+
const APP_READY_PROBE_TIMEOUT = 3e3;
|
|
885
|
+
const APP_READY_POLL_DELAY = 500;
|
|
883
886
|
const CHANGE_ROUTE_DEBUG_ENABLED = process.env.WEAPP_VITE_E2E_CHANGE_ROUTE_DEBUG === "1";
|
|
884
887
|
function sleep(ms) {
|
|
885
888
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -1107,6 +1110,21 @@ var MiniProgram = class extends EventEmitter {
|
|
|
1107
1110
|
const sdkVersion = (await this.send("Tool.getInfo")).SDKVersion;
|
|
1108
1111
|
if (sdkVersion !== "dev" && cmpVersion(sdkVersion, "2.7.3") < 0) throw new Error(`SDKVersion is currently ${sdkVersion}, while automator(${version}) requires at least version 2.7.3`);
|
|
1109
1112
|
}
|
|
1113
|
+
/**
|
|
1114
|
+
* @description 等待小程序 App 域协议可用,避免返回半就绪自动化会话。
|
|
1115
|
+
*/
|
|
1116
|
+
async waitForAppReady(timeout = APP_READY_TIMEOUT) {
|
|
1117
|
+
const startedAt = Date.now();
|
|
1118
|
+
let lastError;
|
|
1119
|
+
while (Date.now() - startedAt <= timeout) try {
|
|
1120
|
+
await this.send("App.captureScreenshot", {}, { timeout: APP_READY_PROBE_TIMEOUT });
|
|
1121
|
+
return;
|
|
1122
|
+
} catch (error) {
|
|
1123
|
+
lastError = error;
|
|
1124
|
+
await sleep(APP_READY_POLL_DELAY);
|
|
1125
|
+
}
|
|
1126
|
+
throw lastError instanceof Error ? lastError : /* @__PURE__ */ new Error(`Timed out waiting App domain readiness after ${timeout}ms`);
|
|
1127
|
+
}
|
|
1110
1128
|
async screenshot(options = {}) {
|
|
1111
1129
|
const { data } = await this.send("App.captureScreenshot");
|
|
1112
1130
|
if (!options.path) return data;
|
|
@@ -1688,7 +1706,7 @@ const AUTOMATOR_LAUNCH_RETRIES = 3;
|
|
|
1688
1706
|
const DEFAULT_RUNTIME_PROVIDER_ENV = "WEAPP_VITE_AUTOMATOR_RUNTIME_PROVIDER";
|
|
1689
1707
|
const LEGACY_RUNTIME_PROVIDER_ENV = "WEAPP_VITE_E2E_RUNTIME_PROVIDER";
|
|
1690
1708
|
const EXTENSION_CONTEXT_INVALIDATED_RE = /Extension context invalidated/i;
|
|
1691
|
-
const RETRYABLE_LAUNCH_PORT_RE = /Wait timed out after \d+ ms|Failed connecting to ws:\/\/127\.0\.0\.1:\d+|Failed connecting to devtools websocket endpoint/i;
|
|
1709
|
+
const RETRYABLE_LAUNCH_PORT_RE = /Wait timed out after \d+ ms|Failed connecting to ws:\/\/127\.0\.0\.1:\d+|Failed connecting to devtools websocket endpoint|Failed to launch wechat web devTools, please make sure cliPath is correctly specified/i;
|
|
1692
1710
|
const WINDOWS_BATCH_CLI_RE = /\.(?:bat|cmd)$/i;
|
|
1693
1711
|
let localhostListenPatched = false;
|
|
1694
1712
|
function isExtensionContextInvalidatedError(error) {
|
|
@@ -1697,6 +1715,33 @@ function isExtensionContextInvalidatedError(error) {
|
|
|
1697
1715
|
function isRetryableAutomatorPortLaunchError(error) {
|
|
1698
1716
|
return error instanceof Error && RETRYABLE_LAUNCH_PORT_RE.test(error.message);
|
|
1699
1717
|
}
|
|
1718
|
+
function retainPortLeaseUntilSessionClose(miniProgram, portLease) {
|
|
1719
|
+
let released = false;
|
|
1720
|
+
const release = async () => {
|
|
1721
|
+
if (released) return;
|
|
1722
|
+
released = true;
|
|
1723
|
+
await portLease.release();
|
|
1724
|
+
};
|
|
1725
|
+
const target = miniProgram;
|
|
1726
|
+
const rawClose = target.close;
|
|
1727
|
+
const rawDisconnect = target.disconnect;
|
|
1728
|
+
if (typeof rawClose !== "function" && typeof rawDisconnect !== "function") return false;
|
|
1729
|
+
if (typeof rawDisconnect === "function") target.disconnect = function disconnectWithPortLeaseRelease() {
|
|
1730
|
+
try {
|
|
1731
|
+
return rawDisconnect.call(this);
|
|
1732
|
+
} finally {
|
|
1733
|
+
release();
|
|
1734
|
+
}
|
|
1735
|
+
};
|
|
1736
|
+
if (typeof rawClose === "function") target.close = async function closeWithPortLeaseRelease() {
|
|
1737
|
+
try {
|
|
1738
|
+
return await rawClose.call(this);
|
|
1739
|
+
} finally {
|
|
1740
|
+
await release();
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
return true;
|
|
1744
|
+
}
|
|
1700
1745
|
function patchNetListenToLoopback() {
|
|
1701
1746
|
if (localhostListenPatched) return;
|
|
1702
1747
|
localhostListenPatched = true;
|
|
@@ -1759,6 +1804,7 @@ var Launcher = class {
|
|
|
1759
1804
|
const { cliPath = await this.resolveCliPath(), timeout = DEFAULT_TIMEOUT, projectConfig = {}, ticket = "", cwd = "", account = "", trustProject = false } = options;
|
|
1760
1805
|
let { args = [], projectPath } = options;
|
|
1761
1806
|
const portLease = await acquireAutomatorPortLease(options.port);
|
|
1807
|
+
let releasePortLeaseOnExit = true;
|
|
1762
1808
|
try {
|
|
1763
1809
|
const port = portLease.port;
|
|
1764
1810
|
if (!cliPath) throw new Error("Wechat web devTools not found, please specify cliPath option");
|
|
@@ -1815,6 +1861,7 @@ var Launcher = class {
|
|
|
1815
1861
|
const candidate = await this.connectTool({ wsEndpoint: `ws://127.0.0.1:${port}` });
|
|
1816
1862
|
try {
|
|
1817
1863
|
await candidate.checkVersion();
|
|
1864
|
+
if (typeof candidate.waitForAppReady === "function") await candidate.waitForAppReady(timeout);
|
|
1818
1865
|
} catch (error) {
|
|
1819
1866
|
candidate.disconnect();
|
|
1820
1867
|
lastConnectError = error;
|
|
@@ -1840,10 +1887,11 @@ var Launcher = class {
|
|
|
1840
1887
|
projectPath: resolvedProjectPath,
|
|
1841
1888
|
wsEndpoint: `ws://127.0.0.1:${port}`
|
|
1842
1889
|
});
|
|
1890
|
+
releasePortLeaseOnExit = !retainPortLeaseUntilSessionClose(resolvedMiniProgram, portLease);
|
|
1843
1891
|
await sleep$1(5e3);
|
|
1844
1892
|
return resolvedMiniProgram;
|
|
1845
1893
|
} finally {
|
|
1846
|
-
await portLease.release();
|
|
1894
|
+
if (releasePortLeaseOnExit) await portLease.release();
|
|
1847
1895
|
}
|
|
1848
1896
|
}
|
|
1849
1897
|
async connect(options) {
|
package/package.json
CHANGED