agent-browser 0.3.1 → 0.3.3

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/src/actions.ts DELETED
@@ -1,1670 +0,0 @@
1
- import type { Page, Frame } from 'playwright-core';
2
- import type { BrowserManager } from './browser.js';
3
- import type {
4
- Command,
5
- Response,
6
- NavigateCommand,
7
- ClickCommand,
8
- TypeCommand,
9
- FillCommand,
10
- CheckCommand,
11
- UncheckCommand,
12
- UploadCommand,
13
- DoubleClickCommand,
14
- FocusCommand,
15
- DragCommand,
16
- FrameCommand,
17
- GetByRoleCommand,
18
- GetByTextCommand,
19
- GetByLabelCommand,
20
- GetByPlaceholderCommand,
21
- PressCommand,
22
- ScreenshotCommand,
23
- EvaluateCommand,
24
- WaitCommand,
25
- ScrollCommand,
26
- SelectCommand,
27
- HoverCommand,
28
- ContentCommand,
29
- TabSwitchCommand,
30
- TabCloseCommand,
31
- WindowNewCommand,
32
- CookiesSetCommand,
33
- StorageGetCommand,
34
- StorageSetCommand,
35
- StorageClearCommand,
36
- DialogCommand,
37
- PdfCommand,
38
- RouteCommand,
39
- RequestsCommand,
40
- DownloadCommand,
41
- GeolocationCommand,
42
- PermissionsCommand,
43
- ViewportCommand,
44
- DeviceCommand,
45
- GetAttributeCommand,
46
- GetTextCommand,
47
- IsVisibleCommand,
48
- IsEnabledCommand,
49
- IsCheckedCommand,
50
- CountCommand,
51
- BoundingBoxCommand,
52
- TraceStartCommand,
53
- TraceStopCommand,
54
- HarStopCommand,
55
- StorageStateSaveCommand,
56
- ConsoleCommand,
57
- ErrorsCommand,
58
- KeyboardCommand,
59
- WheelCommand,
60
- TapCommand,
61
- ClipboardCommand,
62
- HighlightCommand,
63
- ClearCommand,
64
- SelectAllCommand,
65
- InnerTextCommand,
66
- InnerHtmlCommand,
67
- InputValueCommand,
68
- SetValueCommand,
69
- DispatchEventCommand,
70
- AddScriptCommand,
71
- AddStyleCommand,
72
- EmulateMediaCommand,
73
- OfflineCommand,
74
- HeadersCommand,
75
- GetByAltTextCommand,
76
- GetByTitleCommand,
77
- GetByTestIdCommand,
78
- NthCommand,
79
- WaitForUrlCommand,
80
- WaitForLoadStateCommand,
81
- SetContentCommand,
82
- TimezoneCommand,
83
- LocaleCommand,
84
- HttpCredentialsCommand,
85
- MouseMoveCommand,
86
- MouseDownCommand,
87
- MouseUpCommand,
88
- WaitForFunctionCommand,
89
- ScrollIntoViewCommand,
90
- AddInitScriptCommand,
91
- KeyDownCommand,
92
- KeyUpCommand,
93
- InsertTextCommand,
94
- MultiSelectCommand,
95
- WaitForDownloadCommand,
96
- ResponseBodyCommand,
97
- NavigateData,
98
- ScreenshotData,
99
- EvaluateData,
100
- ContentData,
101
- TabListData,
102
- TabNewData,
103
- TabSwitchData,
104
- TabCloseData,
105
- } from './types.js';
106
- import { successResponse, errorResponse } from './protocol.js';
107
-
108
- // Snapshot response type
109
- interface SnapshotData {
110
- snapshot: string;
111
- refs?: Record<string, { role: string; name?: string }>;
112
- }
113
-
114
- /**
115
- * Execute a command and return a response
116
- */
117
- export async function executeCommand(command: Command, browser: BrowserManager): Promise<Response> {
118
- try {
119
- switch (command.action) {
120
- case 'launch':
121
- return await handleLaunch(command, browser);
122
- case 'navigate':
123
- return await handleNavigate(command, browser);
124
- case 'click':
125
- return await handleClick(command, browser);
126
- case 'type':
127
- return await handleType(command, browser);
128
- case 'fill':
129
- return await handleFill(command, browser);
130
- case 'check':
131
- return await handleCheck(command, browser);
132
- case 'uncheck':
133
- return await handleUncheck(command, browser);
134
- case 'upload':
135
- return await handleUpload(command, browser);
136
- case 'dblclick':
137
- return await handleDoubleClick(command, browser);
138
- case 'focus':
139
- return await handleFocus(command, browser);
140
- case 'drag':
141
- return await handleDrag(command, browser);
142
- case 'frame':
143
- return await handleFrame(command, browser);
144
- case 'mainframe':
145
- return await handleMainFrame(command, browser);
146
- case 'getbyrole':
147
- return await handleGetByRole(command, browser);
148
- case 'getbytext':
149
- return await handleGetByText(command, browser);
150
- case 'getbylabel':
151
- return await handleGetByLabel(command, browser);
152
- case 'getbyplaceholder':
153
- return await handleGetByPlaceholder(command, browser);
154
- case 'press':
155
- return await handlePress(command, browser);
156
- case 'screenshot':
157
- return await handleScreenshot(command, browser);
158
- case 'snapshot':
159
- return await handleSnapshot(command, browser);
160
- case 'evaluate':
161
- return await handleEvaluate(command, browser);
162
- case 'wait':
163
- return await handleWait(command, browser);
164
- case 'scroll':
165
- return await handleScroll(command, browser);
166
- case 'select':
167
- return await handleSelect(command, browser);
168
- case 'hover':
169
- return await handleHover(command, browser);
170
- case 'content':
171
- return await handleContent(command, browser);
172
- case 'close':
173
- return await handleClose(command, browser);
174
- case 'tab_new':
175
- return await handleTabNew(command, browser);
176
- case 'tab_list':
177
- return await handleTabList(command, browser);
178
- case 'tab_switch':
179
- return await handleTabSwitch(command, browser);
180
- case 'tab_close':
181
- return await handleTabClose(command, browser);
182
- case 'window_new':
183
- return await handleWindowNew(command, browser);
184
- case 'cookies_get':
185
- return await handleCookiesGet(command, browser);
186
- case 'cookies_set':
187
- return await handleCookiesSet(command, browser);
188
- case 'cookies_clear':
189
- return await handleCookiesClear(command, browser);
190
- case 'storage_get':
191
- return await handleStorageGet(command, browser);
192
- case 'storage_set':
193
- return await handleStorageSet(command, browser);
194
- case 'storage_clear':
195
- return await handleStorageClear(command, browser);
196
- case 'dialog':
197
- return await handleDialog(command, browser);
198
- case 'pdf':
199
- return await handlePdf(command, browser);
200
- case 'route':
201
- return await handleRoute(command, browser);
202
- case 'unroute':
203
- return await handleUnroute(command, browser);
204
- case 'requests':
205
- return await handleRequests(command, browser);
206
- case 'download':
207
- return await handleDownload(command, browser);
208
- case 'geolocation':
209
- return await handleGeolocation(command, browser);
210
- case 'permissions':
211
- return await handlePermissions(command, browser);
212
- case 'viewport':
213
- return await handleViewport(command, browser);
214
- case 'useragent':
215
- return await handleUserAgent(command, browser);
216
- case 'device':
217
- return await handleDevice(command, browser);
218
- case 'back':
219
- return await handleBack(command, browser);
220
- case 'forward':
221
- return await handleForward(command, browser);
222
- case 'reload':
223
- return await handleReload(command, browser);
224
- case 'url':
225
- return await handleUrl(command, browser);
226
- case 'title':
227
- return await handleTitle(command, browser);
228
- case 'getattribute':
229
- return await handleGetAttribute(command, browser);
230
- case 'gettext':
231
- return await handleGetText(command, browser);
232
- case 'isvisible':
233
- return await handleIsVisible(command, browser);
234
- case 'isenabled':
235
- return await handleIsEnabled(command, browser);
236
- case 'ischecked':
237
- return await handleIsChecked(command, browser);
238
- case 'count':
239
- return await handleCount(command, browser);
240
- case 'boundingbox':
241
- return await handleBoundingBox(command, browser);
242
- case 'video_start':
243
- return await handleVideoStart(command, browser);
244
- case 'video_stop':
245
- return await handleVideoStop(command, browser);
246
- case 'trace_start':
247
- return await handleTraceStart(command, browser);
248
- case 'trace_stop':
249
- return await handleTraceStop(command, browser);
250
- case 'har_start':
251
- return await handleHarStart(command, browser);
252
- case 'har_stop':
253
- return await handleHarStop(command, browser);
254
- case 'state_save':
255
- return await handleStateSave(command, browser);
256
- case 'state_load':
257
- return await handleStateLoad(command, browser);
258
- case 'console':
259
- return await handleConsole(command, browser);
260
- case 'errors':
261
- return await handleErrors(command, browser);
262
- case 'keyboard':
263
- return await handleKeyboard(command, browser);
264
- case 'wheel':
265
- return await handleWheel(command, browser);
266
- case 'tap':
267
- return await handleTap(command, browser);
268
- case 'clipboard':
269
- return await handleClipboard(command, browser);
270
- case 'highlight':
271
- return await handleHighlight(command, browser);
272
- case 'clear':
273
- return await handleClear(command, browser);
274
- case 'selectall':
275
- return await handleSelectAll(command, browser);
276
- case 'innertext':
277
- return await handleInnerText(command, browser);
278
- case 'innerhtml':
279
- return await handleInnerHtml(command, browser);
280
- case 'inputvalue':
281
- return await handleInputValue(command, browser);
282
- case 'setvalue':
283
- return await handleSetValue(command, browser);
284
- case 'dispatch':
285
- return await handleDispatch(command, browser);
286
- case 'evalhandle':
287
- return await handleEvalHandle(command, browser);
288
- case 'expose':
289
- return await handleExpose(command, browser);
290
- case 'addscript':
291
- return await handleAddScript(command, browser);
292
- case 'addstyle':
293
- return await handleAddStyle(command, browser);
294
- case 'emulatemedia':
295
- return await handleEmulateMedia(command, browser);
296
- case 'offline':
297
- return await handleOffline(command, browser);
298
- case 'headers':
299
- return await handleHeaders(command, browser);
300
- case 'pause':
301
- return await handlePause(command, browser);
302
- case 'getbyalttext':
303
- return await handleGetByAltText(command, browser);
304
- case 'getbytitle':
305
- return await handleGetByTitle(command, browser);
306
- case 'getbytestid':
307
- return await handleGetByTestId(command, browser);
308
- case 'nth':
309
- return await handleNth(command, browser);
310
- case 'waitforurl':
311
- return await handleWaitForUrl(command, browser);
312
- case 'waitforloadstate':
313
- return await handleWaitForLoadState(command, browser);
314
- case 'setcontent':
315
- return await handleSetContent(command, browser);
316
- case 'timezone':
317
- return await handleTimezone(command, browser);
318
- case 'locale':
319
- return await handleLocale(command, browser);
320
- case 'credentials':
321
- return await handleCredentials(command, browser);
322
- case 'mousemove':
323
- return await handleMouseMove(command, browser);
324
- case 'mousedown':
325
- return await handleMouseDown(command, browser);
326
- case 'mouseup':
327
- return await handleMouseUp(command, browser);
328
- case 'bringtofront':
329
- return await handleBringToFront(command, browser);
330
- case 'waitforfunction':
331
- return await handleWaitForFunction(command, browser);
332
- case 'scrollintoview':
333
- return await handleScrollIntoView(command, browser);
334
- case 'addinitscript':
335
- return await handleAddInitScript(command, browser);
336
- case 'keydown':
337
- return await handleKeyDown(command, browser);
338
- case 'keyup':
339
- return await handleKeyUp(command, browser);
340
- case 'inserttext':
341
- return await handleInsertText(command, browser);
342
- case 'multiselect':
343
- return await handleMultiSelect(command, browser);
344
- case 'waitfordownload':
345
- return await handleWaitForDownload(command, browser);
346
- case 'responsebody':
347
- return await handleResponseBody(command, browser);
348
- default: {
349
- // TypeScript narrows to never here, but we handle it for safety
350
- const unknownCommand = command as { id: string; action: string };
351
- return errorResponse(unknownCommand.id, `Unknown action: ${unknownCommand.action}`);
352
- }
353
- }
354
- } catch (error) {
355
- const message = error instanceof Error ? error.message : String(error);
356
- return errorResponse(command.id, message);
357
- }
358
- }
359
-
360
- async function handleLaunch(
361
- command: Command & { action: 'launch' },
362
- browser: BrowserManager
363
- ): Promise<Response> {
364
- await browser.launch(command);
365
- return successResponse(command.id, { launched: true });
366
- }
367
-
368
- async function handleNavigate(
369
- command: NavigateCommand,
370
- browser: BrowserManager
371
- ): Promise<Response<NavigateData>> {
372
- const page = browser.getPage();
373
- await page.goto(command.url, {
374
- waitUntil: command.waitUntil ?? 'load',
375
- });
376
-
377
- return successResponse(command.id, {
378
- url: page.url(),
379
- title: await page.title(),
380
- });
381
- }
382
-
383
- async function handleClick(command: ClickCommand, browser: BrowserManager): Promise<Response> {
384
- // Support both refs (@e1) and regular selectors
385
- const locator = browser.getLocator(command.selector);
386
-
387
- await locator.click({
388
- button: command.button,
389
- clickCount: command.clickCount,
390
- delay: command.delay,
391
- });
392
-
393
- return successResponse(command.id, { clicked: true });
394
- }
395
-
396
- async function handleType(command: TypeCommand, browser: BrowserManager): Promise<Response> {
397
- const locator = browser.getLocator(command.selector);
398
-
399
- if (command.clear) {
400
- await locator.fill('');
401
- }
402
-
403
- await locator.pressSequentially(command.text, {
404
- delay: command.delay,
405
- });
406
-
407
- return successResponse(command.id, { typed: true });
408
- }
409
-
410
- async function handlePress(command: PressCommand, browser: BrowserManager): Promise<Response> {
411
- const page = browser.getPage();
412
-
413
- if (command.selector) {
414
- await page.press(command.selector, command.key);
415
- } else {
416
- await page.keyboard.press(command.key);
417
- }
418
-
419
- return successResponse(command.id, { pressed: true });
420
- }
421
-
422
- async function handleScreenshot(
423
- command: ScreenshotCommand,
424
- browser: BrowserManager
425
- ): Promise<Response<ScreenshotData>> {
426
- const page = browser.getPage();
427
-
428
- const options: Parameters<Page['screenshot']>[0] = {
429
- fullPage: command.fullPage,
430
- type: command.format ?? 'png',
431
- };
432
-
433
- if (command.format === 'jpeg' && command.quality !== undefined) {
434
- options.quality = command.quality;
435
- }
436
-
437
- let target: Page | ReturnType<Page['locator']> = page;
438
- if (command.selector) {
439
- target = page.locator(command.selector);
440
- }
441
-
442
- if (command.path) {
443
- await target.screenshot({ ...options, path: command.path });
444
- return successResponse(command.id, { path: command.path });
445
- } else {
446
- const buffer = await target.screenshot(options);
447
- return successResponse(command.id, { base64: buffer.toString('base64') });
448
- }
449
- }
450
-
451
- async function handleSnapshot(
452
- command: Command & { action: 'snapshot'; interactive?: boolean; maxDepth?: number; compact?: boolean; selector?: string },
453
- browser: BrowserManager
454
- ): Promise<Response<SnapshotData>> {
455
- // Use enhanced snapshot with refs and optional filtering
456
- const { tree, refs } = await browser.getSnapshot({
457
- interactive: command.interactive,
458
- maxDepth: command.maxDepth,
459
- compact: command.compact,
460
- selector: command.selector,
461
- });
462
-
463
- // Simplify refs for output (just role and name)
464
- const simpleRefs: Record<string, { role: string; name?: string }> = {};
465
- for (const [ref, data] of Object.entries(refs)) {
466
- simpleRefs[ref] = { role: data.role, name: data.name };
467
- }
468
-
469
- return successResponse(command.id, {
470
- snapshot: tree || 'Empty page',
471
- refs: Object.keys(simpleRefs).length > 0 ? simpleRefs : undefined,
472
- });
473
- }
474
-
475
- async function handleEvaluate(
476
- command: EvaluateCommand,
477
- browser: BrowserManager
478
- ): Promise<Response<EvaluateData>> {
479
- const page = browser.getPage();
480
-
481
- // Evaluate the script directly as a string expression
482
- const result = await page.evaluate(command.script);
483
-
484
- return successResponse(command.id, { result });
485
- }
486
-
487
- async function handleWait(command: WaitCommand, browser: BrowserManager): Promise<Response> {
488
- const page = browser.getPage();
489
-
490
- if (command.selector) {
491
- await page.waitForSelector(command.selector, {
492
- state: command.state ?? 'visible',
493
- timeout: command.timeout,
494
- });
495
- } else if (command.timeout) {
496
- await page.waitForTimeout(command.timeout);
497
- } else {
498
- // Default: wait for load state
499
- await page.waitForLoadState('load');
500
- }
501
-
502
- return successResponse(command.id, { waited: true });
503
- }
504
-
505
- async function handleScroll(command: ScrollCommand, browser: BrowserManager): Promise<Response> {
506
- const page = browser.getPage();
507
-
508
- if (command.selector) {
509
- const element = page.locator(command.selector);
510
- await element.scrollIntoViewIfNeeded();
511
-
512
- if (command.x !== undefined || command.y !== undefined) {
513
- await element.evaluate(
514
- (el, { x, y }) => {
515
- el.scrollBy(x ?? 0, y ?? 0);
516
- },
517
- { x: command.x, y: command.y }
518
- );
519
- }
520
- } else {
521
- // Scroll the page
522
- let deltaX = command.x ?? 0;
523
- let deltaY = command.y ?? 0;
524
-
525
- if (command.direction) {
526
- const amount = command.amount ?? 100;
527
- switch (command.direction) {
528
- case 'up':
529
- deltaY = -amount;
530
- break;
531
- case 'down':
532
- deltaY = amount;
533
- break;
534
- case 'left':
535
- deltaX = -amount;
536
- break;
537
- case 'right':
538
- deltaX = amount;
539
- break;
540
- }
541
- }
542
-
543
- await page.evaluate(`window.scrollBy(${deltaX}, ${deltaY})`);
544
- }
545
-
546
- return successResponse(command.id, { scrolled: true });
547
- }
548
-
549
- async function handleSelect(command: SelectCommand, browser: BrowserManager): Promise<Response> {
550
- const locator = browser.getLocator(command.selector);
551
- const values = Array.isArray(command.values) ? command.values : [command.values];
552
-
553
- await locator.selectOption(values);
554
-
555
- return successResponse(command.id, { selected: values });
556
- }
557
-
558
- async function handleHover(command: HoverCommand, browser: BrowserManager): Promise<Response> {
559
- const locator = browser.getLocator(command.selector);
560
- await locator.hover();
561
-
562
- return successResponse(command.id, { hovered: true });
563
- }
564
-
565
- async function handleContent(
566
- command: ContentCommand,
567
- browser: BrowserManager
568
- ): Promise<Response<ContentData>> {
569
- const page = browser.getPage();
570
-
571
- let html: string;
572
- if (command.selector) {
573
- html = await page.locator(command.selector).innerHTML();
574
- } else {
575
- html = await page.content();
576
- }
577
-
578
- return successResponse(command.id, { html });
579
- }
580
-
581
- async function handleClose(
582
- command: Command & { action: 'close' },
583
- browser: BrowserManager
584
- ): Promise<Response> {
585
- await browser.close();
586
- return successResponse(command.id, { closed: true });
587
- }
588
-
589
- async function handleTabNew(
590
- command: Command & { action: 'tab_new' },
591
- browser: BrowserManager
592
- ): Promise<Response<TabNewData>> {
593
- const result = await browser.newTab();
594
- return successResponse(command.id, result);
595
- }
596
-
597
- async function handleTabList(
598
- command: Command & { action: 'tab_list' },
599
- browser: BrowserManager
600
- ): Promise<Response<TabListData>> {
601
- const tabs = await browser.listTabs();
602
- return successResponse(command.id, {
603
- tabs,
604
- active: browser.getActiveIndex(),
605
- });
606
- }
607
-
608
- async function handleTabSwitch(
609
- command: TabSwitchCommand,
610
- browser: BrowserManager
611
- ): Promise<Response<TabSwitchData>> {
612
- const result = browser.switchTo(command.index);
613
- const page = browser.getPage();
614
- return successResponse(command.id, {
615
- ...result,
616
- title: await page.title(),
617
- });
618
- }
619
-
620
- async function handleTabClose(
621
- command: TabCloseCommand,
622
- browser: BrowserManager
623
- ): Promise<Response<TabCloseData>> {
624
- const result = await browser.closeTab(command.index);
625
- return successResponse(command.id, result);
626
- }
627
-
628
- async function handleWindowNew(
629
- command: WindowNewCommand,
630
- browser: BrowserManager
631
- ): Promise<Response<TabNewData>> {
632
- const result = await browser.newWindow(command.viewport);
633
- return successResponse(command.id, result);
634
- }
635
-
636
- // New handlers for enhanced Playwright parity
637
-
638
- async function handleFill(command: FillCommand, browser: BrowserManager): Promise<Response> {
639
- const locator = browser.getLocator(command.selector);
640
- await locator.fill(command.value);
641
- return successResponse(command.id, { filled: true });
642
- }
643
-
644
- async function handleCheck(command: CheckCommand, browser: BrowserManager): Promise<Response> {
645
- const locator = browser.getLocator(command.selector);
646
- await locator.check();
647
- return successResponse(command.id, { checked: true });
648
- }
649
-
650
- async function handleUncheck(command: UncheckCommand, browser: BrowserManager): Promise<Response> {
651
- const locator = browser.getLocator(command.selector);
652
- await locator.uncheck();
653
- return successResponse(command.id, { unchecked: true });
654
- }
655
-
656
- async function handleUpload(command: UploadCommand, browser: BrowserManager): Promise<Response> {
657
- const locator = browser.getLocator(command.selector);
658
- const files = Array.isArray(command.files) ? command.files : [command.files];
659
- await locator.setInputFiles(files);
660
- return successResponse(command.id, { uploaded: files });
661
- }
662
-
663
- async function handleDoubleClick(
664
- command: DoubleClickCommand,
665
- browser: BrowserManager
666
- ): Promise<Response> {
667
- const locator = browser.getLocator(command.selector);
668
- await locator.dblclick();
669
- return successResponse(command.id, { clicked: true });
670
- }
671
-
672
- async function handleFocus(command: FocusCommand, browser: BrowserManager): Promise<Response> {
673
- const locator = browser.getLocator(command.selector);
674
- await locator.focus();
675
- return successResponse(command.id, { focused: true });
676
- }
677
-
678
- async function handleDrag(command: DragCommand, browser: BrowserManager): Promise<Response> {
679
- const frame = browser.getFrame();
680
- await frame.dragAndDrop(command.source, command.target);
681
- return successResponse(command.id, { dragged: true });
682
- }
683
-
684
- async function handleFrame(command: FrameCommand, browser: BrowserManager): Promise<Response> {
685
- await browser.switchToFrame({
686
- selector: command.selector,
687
- name: command.name,
688
- url: command.url,
689
- });
690
- return successResponse(command.id, { switched: true });
691
- }
692
-
693
- async function handleMainFrame(
694
- command: Command & { action: 'mainframe' },
695
- browser: BrowserManager
696
- ): Promise<Response> {
697
- browser.switchToMainFrame();
698
- return successResponse(command.id, { switched: true });
699
- }
700
-
701
- async function handleGetByRole(
702
- command: GetByRoleCommand,
703
- browser: BrowserManager
704
- ): Promise<Response> {
705
- const page = browser.getPage();
706
- const locator = page.getByRole(command.role as any, { name: command.name });
707
-
708
- switch (command.subaction) {
709
- case 'click':
710
- await locator.click();
711
- return successResponse(command.id, { clicked: true });
712
- case 'fill':
713
- await locator.fill(command.value ?? '');
714
- return successResponse(command.id, { filled: true });
715
- case 'check':
716
- await locator.check();
717
- return successResponse(command.id, { checked: true });
718
- case 'hover':
719
- await locator.hover();
720
- return successResponse(command.id, { hovered: true });
721
- }
722
- }
723
-
724
- async function handleGetByText(
725
- command: GetByTextCommand,
726
- browser: BrowserManager
727
- ): Promise<Response> {
728
- const page = browser.getPage();
729
- const locator = page.getByText(command.text, { exact: command.exact });
730
-
731
- switch (command.subaction) {
732
- case 'click':
733
- await locator.click();
734
- return successResponse(command.id, { clicked: true });
735
- case 'hover':
736
- await locator.hover();
737
- return successResponse(command.id, { hovered: true });
738
- }
739
- }
740
-
741
- async function handleGetByLabel(
742
- command: GetByLabelCommand,
743
- browser: BrowserManager
744
- ): Promise<Response> {
745
- const page = browser.getPage();
746
- const locator = page.getByLabel(command.label);
747
-
748
- switch (command.subaction) {
749
- case 'click':
750
- await locator.click();
751
- return successResponse(command.id, { clicked: true });
752
- case 'fill':
753
- await locator.fill(command.value ?? '');
754
- return successResponse(command.id, { filled: true });
755
- case 'check':
756
- await locator.check();
757
- return successResponse(command.id, { checked: true });
758
- }
759
- }
760
-
761
- async function handleGetByPlaceholder(
762
- command: GetByPlaceholderCommand,
763
- browser: BrowserManager
764
- ): Promise<Response> {
765
- const page = browser.getPage();
766
- const locator = page.getByPlaceholder(command.placeholder);
767
-
768
- switch (command.subaction) {
769
- case 'click':
770
- await locator.click();
771
- return successResponse(command.id, { clicked: true });
772
- case 'fill':
773
- await locator.fill(command.value ?? '');
774
- return successResponse(command.id, { filled: true });
775
- }
776
- }
777
-
778
- async function handleCookiesGet(
779
- command: Command & { action: 'cookies_get'; urls?: string[] },
780
- browser: BrowserManager
781
- ): Promise<Response> {
782
- const page = browser.getPage();
783
- const context = page.context();
784
- const cookies = await context.cookies(command.urls);
785
- return successResponse(command.id, { cookies });
786
- }
787
-
788
- async function handleCookiesSet(
789
- command: CookiesSetCommand,
790
- browser: BrowserManager
791
- ): Promise<Response> {
792
- const page = browser.getPage();
793
- const context = page.context();
794
- await context.addCookies(command.cookies);
795
- return successResponse(command.id, { set: true });
796
- }
797
-
798
- async function handleCookiesClear(
799
- command: Command & { action: 'cookies_clear' },
800
- browser: BrowserManager
801
- ): Promise<Response> {
802
- const page = browser.getPage();
803
- const context = page.context();
804
- await context.clearCookies();
805
- return successResponse(command.id, { cleared: true });
806
- }
807
-
808
- async function handleStorageGet(
809
- command: StorageGetCommand,
810
- browser: BrowserManager
811
- ): Promise<Response> {
812
- const page = browser.getPage();
813
- const storageType = command.type === 'local' ? 'localStorage' : 'sessionStorage';
814
-
815
- if (command.key) {
816
- const value = await page.evaluate(`${storageType}.getItem(${JSON.stringify(command.key)})`);
817
- return successResponse(command.id, { key: command.key, value });
818
- } else {
819
- const data = await page.evaluate(`
820
- (() => {
821
- const storage = ${storageType};
822
- const result = {};
823
- for (let i = 0; i < storage.length; i++) {
824
- const key = storage.key(i);
825
- if (key) result[key] = storage.getItem(key);
826
- }
827
- return result;
828
- })()
829
- `);
830
- return successResponse(command.id, { data });
831
- }
832
- }
833
-
834
- async function handleStorageSet(
835
- command: StorageSetCommand,
836
- browser: BrowserManager
837
- ): Promise<Response> {
838
- const page = browser.getPage();
839
- const storageType = command.type === 'local' ? 'localStorage' : 'sessionStorage';
840
-
841
- await page.evaluate(
842
- `${storageType}.setItem(${JSON.stringify(command.key)}, ${JSON.stringify(command.value)})`
843
- );
844
- return successResponse(command.id, { set: true });
845
- }
846
-
847
- async function handleStorageClear(
848
- command: StorageClearCommand,
849
- browser: BrowserManager
850
- ): Promise<Response> {
851
- const page = browser.getPage();
852
- const storageType = command.type === 'local' ? 'localStorage' : 'sessionStorage';
853
-
854
- await page.evaluate(`${storageType}.clear()`);
855
- return successResponse(command.id, { cleared: true });
856
- }
857
-
858
- async function handleDialog(command: DialogCommand, browser: BrowserManager): Promise<Response> {
859
- browser.setDialogHandler(command.response, command.promptText);
860
- return successResponse(command.id, { handler: 'set', response: command.response });
861
- }
862
-
863
- async function handlePdf(command: PdfCommand, browser: BrowserManager): Promise<Response> {
864
- const page = browser.getPage();
865
- await page.pdf({
866
- path: command.path,
867
- format: command.format ?? 'Letter',
868
- });
869
- return successResponse(command.id, { path: command.path });
870
- }
871
-
872
- // Network & Request handlers
873
-
874
- async function handleRoute(command: RouteCommand, browser: BrowserManager): Promise<Response> {
875
- await browser.addRoute(command.url, {
876
- response: command.response,
877
- abort: command.abort,
878
- });
879
- return successResponse(command.id, { routed: command.url });
880
- }
881
-
882
- async function handleUnroute(
883
- command: Command & { action: 'unroute'; url?: string },
884
- browser: BrowserManager
885
- ): Promise<Response> {
886
- await browser.removeRoute(command.url);
887
- return successResponse(command.id, { unrouted: command.url ?? 'all' });
888
- }
889
-
890
- async function handleRequests(
891
- command: RequestsCommand,
892
- browser: BrowserManager
893
- ): Promise<Response> {
894
- if (command.clear) {
895
- browser.clearRequests();
896
- return successResponse(command.id, { cleared: true });
897
- }
898
-
899
- // Start tracking if not already
900
- browser.startRequestTracking();
901
-
902
- const requests = browser.getRequests(command.filter);
903
- return successResponse(command.id, { requests });
904
- }
905
-
906
- async function handleDownload(
907
- command: DownloadCommand,
908
- browser: BrowserManager
909
- ): Promise<Response> {
910
- const page = browser.getPage();
911
-
912
- const [download] = await Promise.all([
913
- page.waitForEvent('download'),
914
- page.click(command.selector),
915
- ]);
916
-
917
- await download.saveAs(command.path);
918
- return successResponse(command.id, {
919
- path: command.path,
920
- suggestedFilename: download.suggestedFilename(),
921
- });
922
- }
923
-
924
- async function handleGeolocation(
925
- command: GeolocationCommand,
926
- browser: BrowserManager
927
- ): Promise<Response> {
928
- await browser.setGeolocation(command.latitude, command.longitude, command.accuracy);
929
- return successResponse(command.id, {
930
- latitude: command.latitude,
931
- longitude: command.longitude,
932
- });
933
- }
934
-
935
- async function handlePermissions(
936
- command: PermissionsCommand,
937
- browser: BrowserManager
938
- ): Promise<Response> {
939
- await browser.setPermissions(command.permissions, command.grant);
940
- return successResponse(command.id, {
941
- permissions: command.permissions,
942
- granted: command.grant,
943
- });
944
- }
945
-
946
- async function handleViewport(
947
- command: ViewportCommand,
948
- browser: BrowserManager
949
- ): Promise<Response> {
950
- await browser.setViewport(command.width, command.height);
951
- return successResponse(command.id, {
952
- width: command.width,
953
- height: command.height,
954
- });
955
- }
956
-
957
- async function handleUserAgent(
958
- command: Command & { action: 'useragent'; userAgent: string },
959
- browser: BrowserManager
960
- ): Promise<Response> {
961
- const page = browser.getPage();
962
- const context = page.context();
963
- // Note: Can't change user agent after context is created, but we can for new pages
964
- return successResponse(command.id, {
965
- note: 'User agent can only be set at launch time. Use device command instead.',
966
- });
967
- }
968
-
969
- async function handleDevice(command: DeviceCommand, browser: BrowserManager): Promise<Response> {
970
- const device = browser.getDevice(command.device);
971
- if (!device) {
972
- const available = browser.listDevices().slice(0, 10).join(', ');
973
- throw new Error(`Unknown device: ${command.device}. Available: ${available}...`);
974
- }
975
-
976
- // Apply device viewport
977
- await browser.setViewport(device.viewport.width, device.viewport.height);
978
-
979
- return successResponse(command.id, {
980
- device: command.device,
981
- viewport: device.viewport,
982
- userAgent: device.userAgent,
983
- });
984
- }
985
-
986
- async function handleBack(
987
- command: Command & { action: 'back' },
988
- browser: BrowserManager
989
- ): Promise<Response> {
990
- const page = browser.getPage();
991
- await page.goBack();
992
- return successResponse(command.id, { url: page.url() });
993
- }
994
-
995
- async function handleForward(
996
- command: Command & { action: 'forward' },
997
- browser: BrowserManager
998
- ): Promise<Response> {
999
- const page = browser.getPage();
1000
- await page.goForward();
1001
- return successResponse(command.id, { url: page.url() });
1002
- }
1003
-
1004
- async function handleReload(
1005
- command: Command & { action: 'reload' },
1006
- browser: BrowserManager
1007
- ): Promise<Response> {
1008
- const page = browser.getPage();
1009
- await page.reload();
1010
- return successResponse(command.id, { url: page.url() });
1011
- }
1012
-
1013
- async function handleUrl(
1014
- command: Command & { action: 'url' },
1015
- browser: BrowserManager
1016
- ): Promise<Response> {
1017
- const page = browser.getPage();
1018
- return successResponse(command.id, { url: page.url() });
1019
- }
1020
-
1021
- async function handleTitle(
1022
- command: Command & { action: 'title' },
1023
- browser: BrowserManager
1024
- ): Promise<Response> {
1025
- const page = browser.getPage();
1026
- const title = await page.title();
1027
- return successResponse(command.id, { title });
1028
- }
1029
-
1030
- async function handleGetAttribute(
1031
- command: GetAttributeCommand,
1032
- browser: BrowserManager
1033
- ): Promise<Response> {
1034
- const locator = browser.getLocator(command.selector);
1035
- const value = await locator.getAttribute(command.attribute);
1036
- return successResponse(command.id, { attribute: command.attribute, value });
1037
- }
1038
-
1039
- async function handleGetText(command: GetTextCommand, browser: BrowserManager): Promise<Response> {
1040
- const locator = browser.getLocator(command.selector);
1041
- const text = await locator.textContent();
1042
- return successResponse(command.id, { text });
1043
- }
1044
-
1045
- async function handleIsVisible(
1046
- command: IsVisibleCommand,
1047
- browser: BrowserManager
1048
- ): Promise<Response> {
1049
- const locator = browser.getLocator(command.selector);
1050
- const visible = await locator.isVisible();
1051
- return successResponse(command.id, { visible });
1052
- }
1053
-
1054
- async function handleIsEnabled(
1055
- command: IsEnabledCommand,
1056
- browser: BrowserManager
1057
- ): Promise<Response> {
1058
- const locator = browser.getLocator(command.selector);
1059
- const enabled = await locator.isEnabled();
1060
- return successResponse(command.id, { enabled });
1061
- }
1062
-
1063
- async function handleIsChecked(
1064
- command: IsCheckedCommand,
1065
- browser: BrowserManager
1066
- ): Promise<Response> {
1067
- const locator = browser.getLocator(command.selector);
1068
- const checked = await locator.isChecked();
1069
- return successResponse(command.id, { checked });
1070
- }
1071
-
1072
- async function handleCount(command: CountCommand, browser: BrowserManager): Promise<Response> {
1073
- const page = browser.getPage();
1074
- const count = await page.locator(command.selector).count();
1075
- return successResponse(command.id, { count });
1076
- }
1077
-
1078
- async function handleBoundingBox(
1079
- command: BoundingBoxCommand,
1080
- browser: BrowserManager
1081
- ): Promise<Response> {
1082
- const page = browser.getPage();
1083
- const box = await page.locator(command.selector).boundingBox();
1084
- return successResponse(command.id, { box });
1085
- }
1086
-
1087
- // Advanced handlers
1088
-
1089
- async function handleVideoStart(
1090
- command: Command & { action: 'video_start'; path: string },
1091
- browser: BrowserManager
1092
- ): Promise<Response> {
1093
- // Video recording requires context-level setup at launch
1094
- // For now, return a note about this limitation
1095
- return successResponse(command.id, {
1096
- note: 'Video recording must be enabled at browser launch. Use --video flag when starting.',
1097
- path: command.path,
1098
- });
1099
- }
1100
-
1101
- async function handleVideoStop(
1102
- command: Command & { action: 'video_stop' },
1103
- browser: BrowserManager
1104
- ): Promise<Response> {
1105
- const page = browser.getPage();
1106
- const video = page.video();
1107
- if (video) {
1108
- const path = await video.path();
1109
- return successResponse(command.id, { path });
1110
- }
1111
- return successResponse(command.id, { note: 'No video recording active' });
1112
- }
1113
-
1114
- async function handleTraceStart(
1115
- command: TraceStartCommand,
1116
- browser: BrowserManager
1117
- ): Promise<Response> {
1118
- await browser.startTracing({
1119
- screenshots: command.screenshots,
1120
- snapshots: command.snapshots,
1121
- });
1122
- return successResponse(command.id, { started: true });
1123
- }
1124
-
1125
- async function handleTraceStop(
1126
- command: TraceStopCommand,
1127
- browser: BrowserManager
1128
- ): Promise<Response> {
1129
- await browser.stopTracing(command.path);
1130
- return successResponse(command.id, { path: command.path });
1131
- }
1132
-
1133
- async function handleHarStart(
1134
- command: Command & { action: 'har_start' },
1135
- browser: BrowserManager
1136
- ): Promise<Response> {
1137
- await browser.startHarRecording();
1138
- browser.startRequestTracking();
1139
- return successResponse(command.id, { started: true });
1140
- }
1141
-
1142
- async function handleHarStop(command: HarStopCommand, browser: BrowserManager): Promise<Response> {
1143
- // HAR recording is handled at context level
1144
- // For now, we save tracked requests as a simplified HAR-like format
1145
- const requests = browser.getRequests();
1146
- return successResponse(command.id, {
1147
- path: command.path,
1148
- requestCount: requests.length,
1149
- });
1150
- }
1151
-
1152
- async function handleStateSave(
1153
- command: StorageStateSaveCommand,
1154
- browser: BrowserManager
1155
- ): Promise<Response> {
1156
- await browser.saveStorageState(command.path);
1157
- return successResponse(command.id, { path: command.path });
1158
- }
1159
-
1160
- async function handleStateLoad(
1161
- command: Command & { action: 'state_load'; path: string },
1162
- browser: BrowserManager
1163
- ): Promise<Response> {
1164
- // Storage state is loaded at context creation
1165
- return successResponse(command.id, {
1166
- note: 'Storage state must be loaded at browser launch. Use --state flag.',
1167
- path: command.path,
1168
- });
1169
- }
1170
-
1171
- async function handleConsole(command: ConsoleCommand, browser: BrowserManager): Promise<Response> {
1172
- if (command.clear) {
1173
- browser.clearConsoleMessages();
1174
- return successResponse(command.id, { cleared: true });
1175
- }
1176
-
1177
- const messages = browser.getConsoleMessages();
1178
- return successResponse(command.id, { messages });
1179
- }
1180
-
1181
- async function handleErrors(command: ErrorsCommand, browser: BrowserManager): Promise<Response> {
1182
- if (command.clear) {
1183
- browser.clearPageErrors();
1184
- return successResponse(command.id, { cleared: true });
1185
- }
1186
-
1187
- const errors = browser.getPageErrors();
1188
- return successResponse(command.id, { errors });
1189
- }
1190
-
1191
- async function handleKeyboard(
1192
- command: KeyboardCommand,
1193
- browser: BrowserManager
1194
- ): Promise<Response> {
1195
- const page = browser.getPage();
1196
- await page.keyboard.press(command.keys);
1197
- return successResponse(command.id, { pressed: command.keys });
1198
- }
1199
-
1200
- async function handleWheel(command: WheelCommand, browser: BrowserManager): Promise<Response> {
1201
- const page = browser.getPage();
1202
-
1203
- if (command.selector) {
1204
- const element = page.locator(command.selector);
1205
- await element.hover();
1206
- }
1207
-
1208
- await page.mouse.wheel(command.deltaX ?? 0, command.deltaY ?? 0);
1209
- return successResponse(command.id, { scrolled: true });
1210
- }
1211
-
1212
- async function handleTap(command: TapCommand, browser: BrowserManager): Promise<Response> {
1213
- const page = browser.getPage();
1214
- await page.tap(command.selector);
1215
- return successResponse(command.id, { tapped: true });
1216
- }
1217
-
1218
- async function handleClipboard(
1219
- command: ClipboardCommand,
1220
- browser: BrowserManager
1221
- ): Promise<Response> {
1222
- const page = browser.getPage();
1223
-
1224
- switch (command.operation) {
1225
- case 'copy':
1226
- await page.keyboard.press('Control+c');
1227
- return successResponse(command.id, { copied: true });
1228
- case 'paste':
1229
- await page.keyboard.press('Control+v');
1230
- return successResponse(command.id, { pasted: true });
1231
- case 'read':
1232
- const text = await page.evaluate('navigator.clipboard.readText()');
1233
- return successResponse(command.id, { text });
1234
- default:
1235
- return errorResponse(command.id, 'Unknown clipboard operation');
1236
- }
1237
- }
1238
-
1239
- async function handleHighlight(
1240
- command: HighlightCommand,
1241
- browser: BrowserManager
1242
- ): Promise<Response> {
1243
- const page = browser.getPage();
1244
- await page.locator(command.selector).highlight();
1245
- return successResponse(command.id, { highlighted: true });
1246
- }
1247
-
1248
- async function handleClear(command: ClearCommand, browser: BrowserManager): Promise<Response> {
1249
- const page = browser.getPage();
1250
- await page.locator(command.selector).clear();
1251
- return successResponse(command.id, { cleared: true });
1252
- }
1253
-
1254
- async function handleSelectAll(
1255
- command: SelectAllCommand,
1256
- browser: BrowserManager
1257
- ): Promise<Response> {
1258
- const page = browser.getPage();
1259
- await page.locator(command.selector).selectText();
1260
- return successResponse(command.id, { selected: true });
1261
- }
1262
-
1263
- async function handleInnerText(
1264
- command: InnerTextCommand,
1265
- browser: BrowserManager
1266
- ): Promise<Response> {
1267
- const page = browser.getPage();
1268
- const text = await page.locator(command.selector).innerText();
1269
- return successResponse(command.id, { text });
1270
- }
1271
-
1272
- async function handleInnerHtml(
1273
- command: InnerHtmlCommand,
1274
- browser: BrowserManager
1275
- ): Promise<Response> {
1276
- const page = browser.getPage();
1277
- const html = await page.locator(command.selector).innerHTML();
1278
- return successResponse(command.id, { html });
1279
- }
1280
-
1281
- async function handleInputValue(
1282
- command: InputValueCommand,
1283
- browser: BrowserManager
1284
- ): Promise<Response> {
1285
- const page = browser.getPage();
1286
- const value = await page.locator(command.selector).inputValue();
1287
- return successResponse(command.id, { value });
1288
- }
1289
-
1290
- async function handleSetValue(
1291
- command: SetValueCommand,
1292
- browser: BrowserManager
1293
- ): Promise<Response> {
1294
- const page = browser.getPage();
1295
- await page.locator(command.selector).fill(command.value);
1296
- return successResponse(command.id, { set: true });
1297
- }
1298
-
1299
- async function handleDispatch(
1300
- command: DispatchEventCommand,
1301
- browser: BrowserManager
1302
- ): Promise<Response> {
1303
- const page = browser.getPage();
1304
- await page.locator(command.selector).dispatchEvent(command.event, command.eventInit);
1305
- return successResponse(command.id, { dispatched: command.event });
1306
- }
1307
-
1308
- async function handleEvalHandle(
1309
- command: Command & { action: 'evalhandle'; script: string },
1310
- browser: BrowserManager
1311
- ): Promise<Response> {
1312
- const page = browser.getPage();
1313
- const handle = await page.evaluateHandle(command.script);
1314
- const result = await handle.jsonValue().catch(() => 'Handle (non-serializable)');
1315
- return successResponse(command.id, { result });
1316
- }
1317
-
1318
- async function handleExpose(
1319
- command: Command & { action: 'expose'; name: string },
1320
- browser: BrowserManager
1321
- ): Promise<Response> {
1322
- const page = browser.getPage();
1323
- await page.exposeFunction(command.name, () => {
1324
- // Exposed function - can be extended
1325
- return `Function ${command.name} called`;
1326
- });
1327
- return successResponse(command.id, { exposed: command.name });
1328
- }
1329
-
1330
- async function handleAddScript(
1331
- command: AddScriptCommand,
1332
- browser: BrowserManager
1333
- ): Promise<Response> {
1334
- const page = browser.getPage();
1335
-
1336
- if (command.content) {
1337
- await page.addScriptTag({ content: command.content });
1338
- } else if (command.url) {
1339
- await page.addScriptTag({ url: command.url });
1340
- }
1341
-
1342
- return successResponse(command.id, { added: true });
1343
- }
1344
-
1345
- async function handleAddStyle(
1346
- command: AddStyleCommand,
1347
- browser: BrowserManager
1348
- ): Promise<Response> {
1349
- const page = browser.getPage();
1350
-
1351
- if (command.content) {
1352
- await page.addStyleTag({ content: command.content });
1353
- } else if (command.url) {
1354
- await page.addStyleTag({ url: command.url });
1355
- }
1356
-
1357
- return successResponse(command.id, { added: true });
1358
- }
1359
-
1360
- async function handleEmulateMedia(
1361
- command: EmulateMediaCommand,
1362
- browser: BrowserManager
1363
- ): Promise<Response> {
1364
- const page = browser.getPage();
1365
- await page.emulateMedia({
1366
- media: command.media,
1367
- colorScheme: command.colorScheme,
1368
- reducedMotion: command.reducedMotion,
1369
- forcedColors: command.forcedColors,
1370
- });
1371
- return successResponse(command.id, { emulated: true });
1372
- }
1373
-
1374
- async function handleOffline(command: OfflineCommand, browser: BrowserManager): Promise<Response> {
1375
- await browser.setOffline(command.offline);
1376
- return successResponse(command.id, { offline: command.offline });
1377
- }
1378
-
1379
- async function handleHeaders(command: HeadersCommand, browser: BrowserManager): Promise<Response> {
1380
- await browser.setExtraHeaders(command.headers);
1381
- return successResponse(command.id, { set: true });
1382
- }
1383
-
1384
- async function handlePause(
1385
- command: Command & { action: 'pause' },
1386
- browser: BrowserManager
1387
- ): Promise<Response> {
1388
- const page = browser.getPage();
1389
- await page.pause();
1390
- return successResponse(command.id, { paused: true });
1391
- }
1392
-
1393
- async function handleGetByAltText(
1394
- command: GetByAltTextCommand,
1395
- browser: BrowserManager
1396
- ): Promise<Response> {
1397
- const page = browser.getPage();
1398
- const locator = page.getByAltText(command.text, { exact: command.exact });
1399
-
1400
- switch (command.subaction) {
1401
- case 'click':
1402
- await locator.click();
1403
- return successResponse(command.id, { clicked: true });
1404
- case 'hover':
1405
- await locator.hover();
1406
- return successResponse(command.id, { hovered: true });
1407
- }
1408
- }
1409
-
1410
- async function handleGetByTitle(
1411
- command: GetByTitleCommand,
1412
- browser: BrowserManager
1413
- ): Promise<Response> {
1414
- const page = browser.getPage();
1415
- const locator = page.getByTitle(command.text, { exact: command.exact });
1416
-
1417
- switch (command.subaction) {
1418
- case 'click':
1419
- await locator.click();
1420
- return successResponse(command.id, { clicked: true });
1421
- case 'hover':
1422
- await locator.hover();
1423
- return successResponse(command.id, { hovered: true });
1424
- }
1425
- }
1426
-
1427
- async function handleGetByTestId(
1428
- command: GetByTestIdCommand,
1429
- browser: BrowserManager
1430
- ): Promise<Response> {
1431
- const page = browser.getPage();
1432
- const locator = page.getByTestId(command.testId);
1433
-
1434
- switch (command.subaction) {
1435
- case 'click':
1436
- await locator.click();
1437
- return successResponse(command.id, { clicked: true });
1438
- case 'fill':
1439
- await locator.fill(command.value ?? '');
1440
- return successResponse(command.id, { filled: true });
1441
- case 'check':
1442
- await locator.check();
1443
- return successResponse(command.id, { checked: true });
1444
- case 'hover':
1445
- await locator.hover();
1446
- return successResponse(command.id, { hovered: true });
1447
- }
1448
- }
1449
-
1450
- async function handleNth(command: NthCommand, browser: BrowserManager): Promise<Response> {
1451
- const page = browser.getPage();
1452
- const base = page.locator(command.selector);
1453
- const locator = command.index === -1 ? base.last() : base.nth(command.index);
1454
-
1455
- switch (command.subaction) {
1456
- case 'click':
1457
- await locator.click();
1458
- return successResponse(command.id, { clicked: true });
1459
- case 'fill':
1460
- await locator.fill(command.value ?? '');
1461
- return successResponse(command.id, { filled: true });
1462
- case 'check':
1463
- await locator.check();
1464
- return successResponse(command.id, { checked: true });
1465
- case 'hover':
1466
- await locator.hover();
1467
- return successResponse(command.id, { hovered: true });
1468
- case 'text':
1469
- const text = await locator.textContent();
1470
- return successResponse(command.id, { text });
1471
- }
1472
- }
1473
-
1474
- async function handleWaitForUrl(
1475
- command: WaitForUrlCommand,
1476
- browser: BrowserManager
1477
- ): Promise<Response> {
1478
- const page = browser.getPage();
1479
- await page.waitForURL(command.url, { timeout: command.timeout });
1480
- return successResponse(command.id, { url: page.url() });
1481
- }
1482
-
1483
- async function handleWaitForLoadState(
1484
- command: WaitForLoadStateCommand,
1485
- browser: BrowserManager
1486
- ): Promise<Response> {
1487
- const page = browser.getPage();
1488
- await page.waitForLoadState(command.state, { timeout: command.timeout });
1489
- return successResponse(command.id, { state: command.state });
1490
- }
1491
-
1492
- async function handleSetContent(
1493
- command: SetContentCommand,
1494
- browser: BrowserManager
1495
- ): Promise<Response> {
1496
- const page = browser.getPage();
1497
- await page.setContent(command.html);
1498
- return successResponse(command.id, { set: true });
1499
- }
1500
-
1501
- async function handleTimezone(
1502
- command: TimezoneCommand,
1503
- browser: BrowserManager
1504
- ): Promise<Response> {
1505
- // Timezone must be set at context level before navigation
1506
- // This is a limitation - it sets for the current context
1507
- const page = browser.getPage();
1508
- await page.context().setGeolocation({ latitude: 0, longitude: 0 }); // Trigger context awareness
1509
- return successResponse(command.id, {
1510
- note: 'Timezone must be set at browser launch. Use --timezone flag.',
1511
- timezone: command.timezone,
1512
- });
1513
- }
1514
-
1515
- async function handleLocale(command: LocaleCommand, browser: BrowserManager): Promise<Response> {
1516
- // Locale must be set at context creation
1517
- return successResponse(command.id, {
1518
- note: 'Locale must be set at browser launch. Use --locale flag.',
1519
- locale: command.locale,
1520
- });
1521
- }
1522
-
1523
- async function handleCredentials(
1524
- command: HttpCredentialsCommand,
1525
- browser: BrowserManager
1526
- ): Promise<Response> {
1527
- const context = browser.getPage().context();
1528
- await context.setHTTPCredentials({
1529
- username: command.username,
1530
- password: command.password,
1531
- });
1532
- return successResponse(command.id, { set: true });
1533
- }
1534
-
1535
- async function handleMouseMove(
1536
- command: MouseMoveCommand,
1537
- browser: BrowserManager
1538
- ): Promise<Response> {
1539
- const page = browser.getPage();
1540
- await page.mouse.move(command.x, command.y);
1541
- return successResponse(command.id, { moved: true, x: command.x, y: command.y });
1542
- }
1543
-
1544
- async function handleMouseDown(
1545
- command: MouseDownCommand,
1546
- browser: BrowserManager
1547
- ): Promise<Response> {
1548
- const page = browser.getPage();
1549
- await page.mouse.down({ button: command.button ?? 'left' });
1550
- return successResponse(command.id, { down: true });
1551
- }
1552
-
1553
- async function handleMouseUp(command: MouseUpCommand, browser: BrowserManager): Promise<Response> {
1554
- const page = browser.getPage();
1555
- await page.mouse.up({ button: command.button ?? 'left' });
1556
- return successResponse(command.id, { up: true });
1557
- }
1558
-
1559
- async function handleBringToFront(
1560
- command: Command & { action: 'bringtofront' },
1561
- browser: BrowserManager
1562
- ): Promise<Response> {
1563
- const page = browser.getPage();
1564
- await page.bringToFront();
1565
- return successResponse(command.id, { focused: true });
1566
- }
1567
-
1568
- async function handleWaitForFunction(
1569
- command: WaitForFunctionCommand,
1570
- browser: BrowserManager
1571
- ): Promise<Response> {
1572
- const page = browser.getPage();
1573
- await page.waitForFunction(command.expression, { timeout: command.timeout });
1574
- return successResponse(command.id, { waited: true });
1575
- }
1576
-
1577
- async function handleScrollIntoView(
1578
- command: ScrollIntoViewCommand,
1579
- browser: BrowserManager
1580
- ): Promise<Response> {
1581
- const page = browser.getPage();
1582
- await page.locator(command.selector).scrollIntoViewIfNeeded();
1583
- return successResponse(command.id, { scrolled: true });
1584
- }
1585
-
1586
- async function handleAddInitScript(
1587
- command: AddInitScriptCommand,
1588
- browser: BrowserManager
1589
- ): Promise<Response> {
1590
- const context = browser.getPage().context();
1591
- await context.addInitScript(command.script);
1592
- return successResponse(command.id, { added: true });
1593
- }
1594
-
1595
- async function handleKeyDown(command: KeyDownCommand, browser: BrowserManager): Promise<Response> {
1596
- const page = browser.getPage();
1597
- await page.keyboard.down(command.key);
1598
- return successResponse(command.id, { down: true, key: command.key });
1599
- }
1600
-
1601
- async function handleKeyUp(command: KeyUpCommand, browser: BrowserManager): Promise<Response> {
1602
- const page = browser.getPage();
1603
- await page.keyboard.up(command.key);
1604
- return successResponse(command.id, { up: true, key: command.key });
1605
- }
1606
-
1607
- async function handleInsertText(
1608
- command: InsertTextCommand,
1609
- browser: BrowserManager
1610
- ): Promise<Response> {
1611
- const page = browser.getPage();
1612
- await page.keyboard.insertText(command.text);
1613
- return successResponse(command.id, { inserted: true });
1614
- }
1615
-
1616
- async function handleMultiSelect(
1617
- command: MultiSelectCommand,
1618
- browser: BrowserManager
1619
- ): Promise<Response> {
1620
- const page = browser.getPage();
1621
- const selected = await page.locator(command.selector).selectOption(command.values);
1622
- return successResponse(command.id, { selected });
1623
- }
1624
-
1625
- async function handleWaitForDownload(
1626
- command: WaitForDownloadCommand,
1627
- browser: BrowserManager
1628
- ): Promise<Response> {
1629
- const page = browser.getPage();
1630
- const download = await page.waitForEvent('download', { timeout: command.timeout });
1631
-
1632
- let filePath: string;
1633
- if (command.path) {
1634
- filePath = command.path;
1635
- await download.saveAs(filePath);
1636
- } else {
1637
- filePath = (await download.path()) || download.suggestedFilename();
1638
- }
1639
-
1640
- return successResponse(command.id, {
1641
- path: filePath,
1642
- filename: download.suggestedFilename(),
1643
- url: download.url(),
1644
- });
1645
- }
1646
-
1647
- async function handleResponseBody(
1648
- command: ResponseBodyCommand,
1649
- browser: BrowserManager
1650
- ): Promise<Response> {
1651
- const page = browser.getPage();
1652
- const response = await page.waitForResponse((resp) => resp.url().includes(command.url), {
1653
- timeout: command.timeout,
1654
- });
1655
-
1656
- const body = await response.text();
1657
- let parsed: unknown = body;
1658
-
1659
- try {
1660
- parsed = JSON.parse(body);
1661
- } catch {
1662
- // Keep as string if not JSON
1663
- }
1664
-
1665
- return successResponse(command.id, {
1666
- url: response.url(),
1667
- status: response.status(),
1668
- body: parsed,
1669
- });
1670
- }