-
Notifications
You must be signed in to change notification settings - Fork 951
Expand file tree
/
Copy pathbase-page-invoke-action.test.ts
More file actions
397 lines (332 loc) · 13.9 KB
/
base-page-invoke-action.test.ts
File metadata and controls
397 lines (332 loc) · 13.9 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { Page } from '@/puppeteer/base-page';
import { describe, expect, it, vi } from 'vitest';
// Mock necessary dependencies to avoid loading AI service dependencies
vi.mock('@midscene/shared/logger', () => ({
getDebug: vi.fn(() => vi.fn()),
logMsg: vi.fn(),
}));
vi.mock('@midscene/core/utils', async () => {
const actual = await vi.importActual('@midscene/core/utils');
return {
...actual,
sleep: vi.fn(() => Promise.resolve()),
};
});
vi.mock('@midscene/shared/node', () => ({
getElementInfosScriptContent: vi.fn(() => ''),
getExtraReturnLogic: vi.fn(() => Promise.resolve('() => ({})')),
}));
vi.mock('@/web-element', () => ({
WebPageContextParser: vi.fn().mockResolvedValue({
tree: { node: null, children: [] },
shotSize: { width: 1024, height: 768 },
shrunkShotToLogicalRatio: 1,
screenshotBase64: 'mock-base64',
}),
}));
vi.mock('@/web-page', () => ({
commonWebActionsForWebPage: vi.fn(() => []),
}));
describe('Page - beforeInvokeAction and afterInvokeAction', () => {
describe('beforeInvokeAction', () => {
it('should call the beforeInvokeAction hook', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const beforeHook = vi.fn();
const page = new Page(mockPage, 'puppeteer', {
beforeInvokeAction: beforeHook,
});
await page.beforeInvokeAction('testAction', { foo: 'bar' });
expect(beforeHook).toHaveBeenCalledTimes(1);
expect(beforeHook).toHaveBeenCalledWith('testAction', { foo: 'bar' });
});
it('should not wait for network idle in beforeInvokeAction', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
await page.beforeInvokeAction('testAction', {});
// beforeInvokeAction no longer waits for network idle
expect(mockPage.waitForNetworkIdle).not.toHaveBeenCalled();
expect(mockPage.waitForSelector).not.toHaveBeenCalled();
});
it('should execute immediately without waiting', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockImplementation(() => {
return new Promise((resolve) => setTimeout(() => resolve(true), 100));
}),
waitForNetworkIdle: vi.fn().mockImplementation(() => {
return new Promise((resolve) => setTimeout(() => resolve(true), 100));
}),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
const startTime = Date.now();
await page.beforeInvokeAction('testAction', {});
const duration = Date.now() - startTime;
// Should execute immediately without waiting
expect(duration).toBeLessThan(50);
expect(mockPage.waitForSelector).not.toHaveBeenCalled();
expect(mockPage.waitForNetworkIdle).not.toHaveBeenCalled();
});
it('should call the beforeInvokeAction hook without waiting', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const callOrder: string[] = [];
mockPage.waitForSelector = vi.fn().mockImplementation(() => {
callOrder.push('waitForSelector');
return Promise.resolve(true);
});
mockPage.waitForNetworkIdle = vi.fn().mockImplementation(() => {
callOrder.push('waitForNetworkIdle');
return Promise.resolve(true);
});
const beforeHook = vi.fn().mockImplementation(() => {
callOrder.push('beforeHook');
});
const page = new Page(mockPage, 'puppeteer', {
beforeInvokeAction: beforeHook,
});
await page.beforeInvokeAction('testAction', { foo: 'bar' });
// beforeInvokeAction should only call the hook, no waiting
expect(callOrder).toEqual(['beforeHook']);
expect(beforeHook).toHaveBeenCalledWith('testAction', { foo: 'bar' });
});
it('should work without hook configured', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn(),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
await page.beforeInvokeAction('testAction', {});
// Should complete without error even when no hook is configured
expect(mockPage.waitForSelector).not.toHaveBeenCalled();
expect(mockPage.waitForNetworkIdle).not.toHaveBeenCalled();
});
});
describe('afterInvokeAction', () => {
it('should wait for navigation with default timeout', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
await page.afterInvokeAction('testAction', {});
expect(mockPage.waitForSelector).toHaveBeenCalledTimes(1);
expect(mockPage.waitForSelector).toHaveBeenCalledWith('html', {
timeout: 5000, // DEFAULT_WAIT_FOR_NAVIGATION_TIMEOUT
});
});
it('should wait for network idle for puppeteer', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
await page.afterInvokeAction('testAction', {});
expect(mockPage.waitForNetworkIdle).toHaveBeenCalledTimes(1);
expect(mockPage.waitForNetworkIdle).toHaveBeenCalledWith({
idleTime: 200,
concurrency: 2, // DEFAULT_WAIT_FOR_NETWORK_IDLE_CONCURRENCY
timeout: 2000, // DEFAULT_WAIT_FOR_NETWORK_IDLE_TIMEOUT
});
});
it('should call the afterInvokeAction hook after waiting', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const callOrder: string[] = [];
mockPage.waitForSelector = vi.fn().mockImplementation(() => {
callOrder.push('waitForSelector');
return Promise.resolve(true);
});
mockPage.waitForNetworkIdle = vi.fn().mockImplementation(() => {
callOrder.push('waitForNetworkIdle');
return Promise.resolve(true);
});
const afterHook = vi.fn().mockImplementation(() => {
callOrder.push('afterHook');
});
const page = new Page(mockPage, 'puppeteer', {
afterInvokeAction: afterHook,
});
await page.afterInvokeAction('testAction', { foo: 'bar' });
// Both wait methods should be called before the hook
expect(callOrder).toContain('waitForSelector');
expect(callOrder).toContain('waitForNetworkIdle');
expect(callOrder).toContain('afterHook');
const afterHookIndex = callOrder.indexOf('afterHook');
const waitSelectorIndex = callOrder.indexOf('waitForSelector');
const waitNetworkIndex = callOrder.indexOf('waitForNetworkIdle');
expect(waitSelectorIndex).toBeLessThan(afterHookIndex);
expect(waitNetworkIndex).toBeLessThan(afterHookIndex);
expect(afterHook).toHaveBeenCalledWith('testAction', { foo: 'bar' });
});
it('should skip waiting for navigation when timeout is 0', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn(),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer', {
waitForNavigationTimeout: 0,
});
await page.afterInvokeAction('testAction', {});
// waitForSelector should not be called when timeout is 0
expect(mockPage.waitForSelector).not.toHaveBeenCalled();
});
it('should skip waiting for network idle when timeout is 0', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn(),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer', {
waitForNetworkIdleTimeout: 0,
});
await page.afterInvokeAction('testAction', {});
// waitForNetworkIdle should not be called when timeout is 0
expect(mockPage.waitForNetworkIdle).not.toHaveBeenCalled();
});
it('should use the configured network idle timeout', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer', {
waitForNetworkIdleTimeout: 4321,
});
await page.afterInvokeAction('testAction', {});
expect(mockPage.waitForNetworkIdle).toHaveBeenCalledWith({
idleTime: 200,
concurrency: 2,
timeout: 4321,
});
});
it('should handle navigation timeout gracefully', async () => {
const consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {});
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi
.fn()
.mockRejectedValue(new Error('Timeout waiting for selector')),
waitForNetworkIdle: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
// Should not throw error when navigation times out
await expect(
page.afterInvokeAction('testAction', {}),
).resolves.toBeUndefined();
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('Waiting for the "navigation" has timed out'),
);
consoleWarnSpy.mockRestore();
});
it('should handle network idle timeout gracefully', async () => {
const consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {});
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
waitForNetworkIdle: vi
.fn()
.mockRejectedValue(new Error('Timeout waiting for network idle')),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'puppeteer');
// Should not throw error when network idle times out
await expect(
page.afterInvokeAction('testAction', {}),
).resolves.toBeUndefined();
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('Waiting for the "network idle" has timed out'),
);
consoleWarnSpy.mockRestore();
});
});
describe('playwright interface', () => {
it('should work with playwright interface in beforeInvokeAction', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'playwright');
await page.beforeInvokeAction('testAction', {});
// beforeInvokeAction no longer waits
expect(mockPage.waitForSelector).not.toHaveBeenCalled();
});
it('should work with playwright interface in afterInvokeAction', async () => {
const mockPage = {
url: () => 'http://example.com',
mouse: { move: vi.fn() },
keyboard: { down: vi.fn(), up: vi.fn(), press: vi.fn(), type: vi.fn() },
waitForSelector: vi.fn().mockResolvedValue(true),
evaluate: vi.fn(),
} as any;
const page = new Page(mockPage, 'playwright');
await page.afterInvokeAction('testAction', {});
// Should call waitForSelector for playwright
expect(mockPage.waitForSelector).toHaveBeenCalledWith('html', {
timeout: 5000,
});
});
});
});