Skip to content

Commit 7748783

Browse files
committed
feat: add interaction commands
1 parent 00541b2 commit 7748783

4 files changed

Lines changed: 660 additions & 8 deletions

File tree

src/browser/daemon.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import os from 'os';
44
import path from 'path';
55
import {chromium} from 'playwright-core';
66
import {clear_connection_state, ensure_connected as ensure_browser_connected} from './connection';
7+
import {
8+
handle_check,
9+
handle_click,
10+
handle_fill,
11+
handle_get_html,
12+
handle_get_text,
13+
handle_hover,
14+
handle_scroll,
15+
handle_select,
16+
handle_type,
17+
} from './interaction';
718
import {parse_daemon_request} from './ipc';
819
import {take_screenshot} from './screenshot';
920
import {capture_snapshot} from './snapshot';
@@ -27,6 +38,7 @@ type Json_object = Record<string, unknown>;
2738
type Tracked_request = {
2839
method: string;
2940
url: string;
41+
resource_type?: string;
3042
status?: number;
3143
};
3244

@@ -491,13 +503,25 @@ class BrowserDaemon {
491503
{
492504
case 'back':
493505
return this.handle_history_navigation('back');
506+
case 'check':
507+
return handle_check(await this.ensure_connected(), request.params, true);
508+
case 'click':
509+
return handle_click(await this.ensure_connected(), request.params);
494510
case 'close':
495511
await this.close_browser();
496512
return {closed: true};
497513
case 'cookies':
498514
return this.handle_cookies();
515+
case 'fill':
516+
return handle_fill(await this.ensure_connected(), request.params);
499517
case 'forward':
500518
return this.handle_history_navigation('forward');
519+
case 'get_html':
520+
return handle_get_html(await this.ensure_connected(), request.params);
521+
case 'get_text':
522+
return handle_get_text(await this.ensure_connected(), request.params);
523+
case 'hover':
524+
return handle_hover(await this.ensure_connected(), request.params);
501525
case 'navigate':
502526
return this.handle_navigate(request.params);
503527
case 'network':
@@ -508,10 +532,18 @@ class BrowserDaemon {
508532
return this.handle_reload();
509533
case 'screenshot':
510534
return this.handle_screenshot(request.params);
535+
case 'scroll':
536+
return handle_scroll(await this.ensure_connected(), request.params);
537+
case 'select':
538+
return handle_select(await this.ensure_connected(), request.params);
511539
case 'snapshot':
512540
return this.handle_snapshot(request.params);
513541
case 'status':
514542
return this.handle_status();
543+
case 'type':
544+
return handle_type(await this.ensure_connected(), request.params);
545+
case 'uncheck':
546+
return handle_check(await this.ensure_connected(), request.params, false);
515547
default:
516548
throw new Error(
517549
`Unknown daemon action "${request.action}".`
@@ -526,12 +558,28 @@ class BrowserDaemon {
526558
throw new Error('Navigate requires a non-empty "url" parameter.');
527559
}
528560

561+
const new_endpoint = params?.['cdp_endpoint'];
562+
if (new_endpoint !== undefined)
563+
{
564+
if (typeof new_endpoint != 'string' || !new_endpoint.trim())
565+
throw new Error('Navigate "cdp_endpoint" must be a non-empty string.');
566+
if (new_endpoint.trim() !== this.state.cdp_endpoint)
567+
await this.switch_cdp_endpoint(new_endpoint.trim());
568+
}
569+
529570
const page = await this.ensure_connected();
530571
this.state.dom_refs.clear();
572+
this.state.requests.clear();
531573
const response = await page.goto(url, {waitUntil: 'load'});
532574
return this.create_navigation_result(page, response);
533575
}
534576

577+
private async switch_cdp_endpoint(new_endpoint: string): Promise<void> {
578+
await this.close_browser();
579+
this.state.cdp_endpoint = new_endpoint;
580+
this.state.requests.clear();
581+
}
582+
535583
private async handle_history_navigation(direction: 'back'|'forward'){
536584
const page = await this.ensure_connected();
537585
this.state.dom_refs.clear();
@@ -599,12 +647,21 @@ class BrowserDaemon {
599647
);
600648
}
601649

650+
const wrap = params?.['wrap'];
651+
if (wrap !== undefined && typeof wrap != 'boolean')
652+
{
653+
throw new Error(
654+
'Snapshot "wrap" parameter must be a boolean when provided.'
655+
);
656+
}
657+
602658
const page = await this.ensure_connected();
603659
const result = await capture_snapshot(page, {
604660
compact: compact === true,
605661
depth: depth as number|undefined,
606662
interactive: interactive === true,
607663
selector: typeof selector == 'string' ? selector.trim() : undefined,
664+
wrap: wrap === true,
608665
});
609666

610667
this.state.dom_refs.clear();
@@ -620,6 +677,7 @@ class BrowserDaemon {
620677
snapshot: result.snapshot,
621678
title: result.title,
622679
url: result.url,
680+
wrap: result.wrap,
623681
};
624682
}
625683

@@ -741,6 +799,7 @@ class BrowserDaemon {
741799
this.state.requests.set(request_id, {
742800
method: request.method(),
743801
url: request.url(),
802+
resource_type: request.resourceType(),
744803
});
745804
this.trim_tracked_requests();
746805
}

0 commit comments

Comments
 (0)