-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.ts
More file actions
279 lines (241 loc) · 9.23 KB
/
execute.ts
File metadata and controls
279 lines (241 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import type { TauriAPIs } from '@wdio/native-types';
import { createLogger } from '@wdio/native-utils';
import type { TauriCommandContext, TauriResult } from '../types.js';
const log = createLogger('tauri-service', 'service');
// WeakMap to store plugin availability per browser session
// Automatically cleans up when browser objects are garbage collected
const pluginAvailabilityCache = new WeakMap<WebdriverIO.Browser, boolean>();
/**
* Execute JavaScript code in the Tauri frontend context with access to Tauri APIs
* Matches Electron's execute pattern: accepts functions or strings, passes Tauri APIs as first parameter
*/
export async function execute<ReturnValue, InnerArguments extends unknown[]>(
browser: WebdriverIO.Browser,
script: string | ((tauri: TauriAPIs, ...innerArgs: InnerArguments) => ReturnValue),
...args: InnerArguments
): Promise<ReturnValue> {
/**
* parameter check
*/
if (typeof script !== 'string' && typeof script !== 'function') {
throw new Error('Expecting script to be type of "string" or "function"');
}
if (!browser) {
throw new Error('WDIO browser is not yet initialised');
}
// Check cache first using WeakMap - automatically cleans up when browser is GC'd
if (!pluginAvailabilityCache.get(browser)) {
// Check if plugin is available with retry logic (handles async module loading)
// Use execute (sync) since executeAsync has issues with some embedded providers
const maxAttempts = 100; // 100 attempts * 50ms = 5 seconds max
const retryInterval = 50; // ms
let pluginAvailable = false;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result: unknown = await browser.execute(() => {
// @ts-expect-error - Plugin API injected at runtime
return typeof window.wdioTauri !== 'undefined' && typeof window.wdioTauri.execute === 'function';
});
pluginAvailable = result === true;
if (pluginAvailable) {
break;
}
// Wait before retrying
await new Promise((resolve) => setTimeout(resolve, retryInterval));
}
if (!pluginAvailable) {
throw new Error(
'Tauri plugin not available. Make sure @wdio/tauri-plugin is installed and registered in your Tauri app.',
);
}
// Cache the successful check using browser object as key
pluginAvailabilityCache.set(browser, true);
log.debug('Plugin availability cached for browser session');
} else {
log.debug('Plugin availability cached, skipping check');
}
// Convert function to string - keep parameters intact, plugin will handle escaping
// For functions: use .toString() (produces valid JS function source)
// For strings: send as-is (Rust handles proper escaping when args present)
const scriptString = typeof script === 'function' ? script.toString() : script;
// Execute via plugin's execute command with better error handling
// The plugin will inject the Tauri APIs object as the first argument
const result = await browser.execute(
async function executeWithinTauri(script: string, ...args) {
// @ts-expect-error - Running in browser context
if (typeof window === 'undefined') {
return JSON.stringify({ __wdio_error__: 'window is undefined' });
}
// @ts-expect-error - Running in browser context
if (typeof window.wdioTauri === 'undefined') {
return JSON.stringify({ __wdio_error__: 'window.wdioTauri is undefined' });
}
// @ts-expect-error - Running in browser context
if (typeof window.wdioTauri.execute !== 'function') {
// @ts-expect-error - Running in browser context
return JSON.stringify({ __wdio_error__: `window.wdioTauri.execute is ${typeof window.wdioTauri.execute}` });
}
try {
// @ts-expect-error - Running in browser context
const execResult = window.wdioTauri.execute(script, ...args);
// Handle Promise results - await them in browser context
if (execResult && typeof execResult.then === 'function') {
try {
const awaited = await execResult;
return JSON.stringify({ __wdio_value__: awaited });
} catch (promiseError) {
return JSON.stringify({
__wdio_error__: `Promise error: ${promiseError instanceof Error ? promiseError.message : String(promiseError)}`,
});
}
}
return JSON.stringify({ __wdio_value__: execResult });
} catch (error) {
return JSON.stringify({
__wdio_error__: `Execute call error: ${error instanceof Error ? error.message : String(error)}`,
});
}
},
scriptString,
...args,
);
// Extract result or error from wrapped response
let parsed: { __wdio_error__?: string; __wdio_value__?: unknown } | undefined;
if (result && typeof result === 'string') {
try {
parsed = JSON.parse(result) as { __wdio_error__?: string; __wdio_value__?: unknown };
} catch (parseError) {
throw new Error(
`Failed to parse execute result: ${parseError instanceof Error ? parseError.message : String(parseError)}, raw result: ${result}`,
);
}
}
// Check for script errors AFTER parsing (outside try/catch to avoid re-wrapping)
if (parsed?.__wdio_error__) {
throw new Error(parsed.__wdio_error__);
}
if (parsed?.__wdio_value__ !== undefined) {
log.debug(`Execute result:`, parsed.__wdio_value__);
return parsed.__wdio_value__ as ReturnValue;
}
log.debug(`Execute result:`, result);
return result as ReturnValue;
}
/**
* Execute a Tauri command (legacy method - kept for backward compatibility)
* @deprecated Use execute() instead
*/
export async function executeTauriCommand<T = unknown>(
browser: WebdriverIO.Browser,
command: string,
...args: unknown[]
): Promise<TauriResult<T>> {
log.debug(`Executing Tauri command: ${command} with args:`, args);
try {
const result = await execute(browser, ({ core }) => core.invoke(command, ...args));
return {
ok: true,
value: result as T,
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Tauri command failed: ${errorMessage}`);
return {
ok: false,
error: errorMessage,
};
}
}
/**
* Execute a Tauri command with timeout
*/
export async function executeTauriCommandWithTimeout<T = unknown>(
browser: WebdriverIO.Browser,
command: string,
timeout: number = 30000,
...args: unknown[]
): Promise<TauriResult<T>> {
log.debug(`Executing Tauri command with timeout ${timeout}ms: ${command}`);
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => {
reject(new Error(`Tauri command timeout after ${timeout}ms`));
}, timeout);
});
try {
const result = await Promise.race([executeTauriCommand<T>(browser, command, ...args), timeoutPromise]);
return result;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
log.error(`Tauri command timeout or error: ${errorMessage}`);
return {
ok: false,
error: errorMessage,
};
}
}
/**
* Execute multiple Tauri commands in sequence
*/
export async function executeTauriCommands<T = unknown>(
browser: WebdriverIO.Browser,
commands: TauriCommandContext[],
): Promise<TauriResult<T>[]> {
log.debug(`Executing ${commands.length} Tauri commands in sequence`);
const results: TauriResult<T>[] = [];
for (const { command, args, timeout } of commands) {
const result = timeout
? await executeTauriCommandWithTimeout<T>(browser, command, timeout, ...args)
: await executeTauriCommand<T>(browser, command, ...args);
results.push(result);
// Stop on first failure
if (!result.ok) {
log.warn(`Stopping command execution due to failure: ${result.error}`);
break;
}
}
return results;
}
/**
* Execute Tauri commands in parallel
*/
export async function executeTauriCommandsParallel<T = unknown>(
browser: WebdriverIO.Browser,
commands: TauriCommandContext[],
): Promise<TauriResult<T>[]> {
log.debug(`Executing ${commands.length} Tauri commands in parallel`);
const promises = commands.map(({ command, args, timeout }) =>
timeout
? executeTauriCommandWithTimeout<T>(browser, command, timeout, ...args)
: executeTauriCommand<T>(browser, command, ...args),
);
return Promise.all(promises);
}
/**
* Check if Tauri API is available
*/
export async function isTauriApiAvailable(browser: WebdriverIO.Browser): Promise<boolean> {
try {
const result = await browser.execute(() => {
// @ts-expect-error - Tauri API injected at runtime
return typeof window.__TAURI__ !== 'undefined';
});
return Boolean(result);
} catch (error) {
log.debug(`Tauri API not available: ${error}`);
return false;
}
}
/**
* Get Tauri version
*/
export async function getTauriVersion(browser: WebdriverIO.Browser): Promise<TauriResult<string>> {
return executeTauriCommand<string>(browser, 'get_tauri_version');
}
/**
* Get Tauri app information
*/
export async function getTauriAppInfo(
browser: WebdriverIO.Browser,
): Promise<TauriResult<{ name: string; version: string }>> {
return executeTauriCommand<{ name: string; version: string }>(browser, 'get_app_info');
}