-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.go
More file actions
680 lines (582 loc) · 18.3 KB
/
actions.go
File metadata and controls
680 lines (582 loc) · 18.3 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// SPDX-License-Identifier: EUPL-1.2
package webview
import (
"context"
"time"
core "dappco.re/go/core"
coreerr "dappco.re/go/log"
)
// Action represents a browser action that can be performed.
type Action interface {
Execute(ctx context.Context, wv *Webview) error
}
// ClickAction represents a click action.
type ClickAction struct {
Selector string
}
// Execute performs the click action.
func (a ClickAction) Execute(ctx context.Context, wv *Webview) error {
return wv.click(ctx, a.Selector)
}
// TypeAction represents a typing action.
type TypeAction struct {
Selector string
Text string
}
// Execute performs the type action.
func (a TypeAction) Execute(ctx context.Context, wv *Webview) error {
return wv.typeText(ctx, a.Selector, a.Text)
}
// NavigateAction represents a navigation action.
type NavigateAction struct {
URL string
}
// Execute performs the navigate action.
func (a NavigateAction) Execute(ctx context.Context, wv *Webview) error {
return wv.navigate(ctx, a.URL, "NavigateAction.Execute")
}
// WaitAction represents a wait action.
type WaitAction struct {
Duration time.Duration
}
// Execute performs the wait action.
func (a WaitAction) Execute(ctx context.Context, wv *Webview) error {
timer := time.NewTimer(a.Duration)
defer timer.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}
// WaitForSelectorAction represents waiting for a selector.
type WaitForSelectorAction struct {
Selector string
}
// Execute waits for the selector to appear.
func (a WaitForSelectorAction) Execute(ctx context.Context, wv *Webview) error {
return wv.waitForSelector(ctx, a.Selector)
}
// ScrollAction represents a scroll action.
type ScrollAction struct {
X int
Y int
}
// Execute performs the scroll action.
func (a ScrollAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("window.scrollTo(%d, %d)", a.X, a.Y)
_, err := wv.evaluate(ctx, script)
return err
}
// ScrollIntoViewAction scrolls an element into view.
type ScrollIntoViewAction struct {
Selector string
}
// Execute scrolls the element into view.
func (a ScrollIntoViewAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("document.querySelector(%q)?.scrollIntoView({behavior: 'smooth', block: 'center'})", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// FocusAction focuses an element.
type FocusAction struct {
Selector string
}
// Execute focuses the element.
func (a FocusAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("document.querySelector(%q)?.focus()", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// BlurAction removes focus from an element.
type BlurAction struct {
Selector string
}
// Execute removes focus from the element.
func (a BlurAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("document.querySelector(%q)?.blur()", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// ClearAction clears the value of an input element.
type ClearAction struct {
Selector string
}
// Execute clears the input value.
func (a ClearAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = '';
el.dispatchEvent(new Event('input', {bubbles: true}));
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// SelectAction selects an option in a select element.
type SelectAction struct {
Selector string
Value string
}
// Execute selects the option.
func (a SelectAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = %q;
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// CheckAction checks or unchecks a checkbox.
type CheckAction struct {
Selector string
Checked bool
}
// Execute checks/unchecks the checkbox.
func (a CheckAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el && el.checked !== %t) {
el.click();
}
`, a.Selector, a.Checked)
_, err := wv.evaluate(ctx, script)
return err
}
// HoverAction hovers over an element.
type HoverAction struct {
Selector string
}
// Execute hovers over the element.
func (a HoverAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
return coreerr.E("HoverAction.Execute", "element has no bounding box", nil)
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseMoved",
"x": x,
"y": y,
})
return err
}
// DoubleClickAction double-clicks an element.
type DoubleClickAction struct {
Selector string
}
// Execute double-clicks the element.
func (a DoubleClickAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
// Fallback to JavaScript
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el) {
const event = new MouseEvent('dblclick', {bubbles: true, cancelable: true, view: window});
el.dispatchEvent(event);
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
// Double click sequence
for i := range 2 {
for _, eventType := range []string{"mousePressed", "mouseReleased"} {
_, err := wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": eventType,
"x": x,
"y": y,
"button": "left",
"clickCount": i + 1,
})
if err != nil {
return err
}
}
}
return nil
}
// RightClickAction right-clicks an element.
type RightClickAction struct {
Selector string
}
// Execute right-clicks the element.
func (a RightClickAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
// Fallback to JavaScript
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el) {
const event = new MouseEvent('contextmenu', {bubbles: true, cancelable: true, view: window});
el.dispatchEvent(event);
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
for _, eventType := range []string{"mousePressed", "mouseReleased"} {
_, err := wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": eventType,
"x": x,
"y": y,
"button": "right",
"clickCount": 1,
})
if err != nil {
return err
}
}
return nil
}
// PressKeyAction presses a key.
type PressKeyAction struct {
Key string // e.g., "Enter", "Tab", "Escape"
}
// Execute presses the key.
func (a PressKeyAction) Execute(ctx context.Context, wv *Webview) error {
// Map common key names to CDP key codes
keyMap := map[string]struct {
code string
keyCode int
text string
unmodified string
}{
"Enter": {"Enter", 13, "\r", "\r"},
"Tab": {"Tab", 9, "", ""},
"Escape": {"Escape", 27, "", ""},
"Backspace": {"Backspace", 8, "", ""},
"Delete": {"Delete", 46, "", ""},
"ArrowUp": {"ArrowUp", 38, "", ""},
"ArrowDown": {"ArrowDown", 40, "", ""},
"ArrowLeft": {"ArrowLeft", 37, "", ""},
"ArrowRight": {"ArrowRight", 39, "", ""},
"Home": {"Home", 36, "", ""},
"End": {"End", 35, "", ""},
"PageUp": {"PageUp", 33, "", ""},
"PageDown": {"PageDown", 34, "", ""},
}
keyInfo, ok := keyMap[a.Key]
if !ok {
// For simple characters, just send key events
_, err := wv.client.Call(ctx, "Input.dispatchKeyEvent", map[string]any{
"type": "keyDown",
"text": a.Key,
})
if err != nil {
return err
}
_, err = wv.client.Call(ctx, "Input.dispatchKeyEvent", map[string]any{
"type": "keyUp",
})
return err
}
params := map[string]any{
"type": "keyDown",
"code": keyInfo.code,
"key": a.Key,
"windowsVirtualKeyCode": keyInfo.keyCode,
"nativeVirtualKeyCode": keyInfo.keyCode,
}
if keyInfo.text != "" {
params["text"] = keyInfo.text
params["unmodifiedText"] = keyInfo.unmodified
}
_, err := wv.client.Call(ctx, "Input.dispatchKeyEvent", params)
if err != nil {
return err
}
params["type"] = "keyUp"
delete(params, "text")
delete(params, "unmodifiedText")
_, err = wv.client.Call(ctx, "Input.dispatchKeyEvent", params)
return err
}
// SetAttributeAction sets an attribute on an element.
type SetAttributeAction struct {
Selector string
Attribute string
Value string
}
// Execute sets the attribute.
func (a SetAttributeAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("document.querySelector(%q)?.setAttribute(%q, %q)", a.Selector, a.Attribute, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// RemoveAttributeAction removes an attribute from an element.
type RemoveAttributeAction struct {
Selector string
Attribute string
}
// Execute removes the attribute.
func (a RemoveAttributeAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf("document.querySelector(%q)?.removeAttribute(%q)", a.Selector, a.Attribute)
_, err := wv.evaluate(ctx, script)
return err
}
// SetValueAction sets the value of an input element.
type SetValueAction struct {
Selector string
Value string
}
// Execute sets the value.
func (a SetValueAction) Execute(ctx context.Context, wv *Webview) error {
script := core.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = %q;
el.dispatchEvent(new Event('input', {bubbles: true}));
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// UploadFileAction uploads files into a file input resolved by selector.
type UploadFileAction struct {
Selector string
FilePaths []string
}
// Execute uploads files into the matching file input.
func (a UploadFileAction) Execute(ctx context.Context, wv *Webview) error {
if wv == nil {
return coreerr.E("UploadFileAction.Execute", "webview is required", nil)
}
return wv.uploadFile(ctx, a.Selector, a.FilePaths)
}
// DragAndDropAction drags one element onto another.
type DragAndDropAction struct {
SourceSelector string
TargetSelector string
}
// Execute drags the source element onto the target element.
func (a DragAndDropAction) Execute(ctx context.Context, wv *Webview) error {
if wv == nil {
return coreerr.E("DragAndDropAction.Execute", "webview is required", nil)
}
return wv.dragAndDrop(ctx, a.SourceSelector, a.TargetSelector)
}
// ActionSequence represents a sequence of actions to execute.
type ActionSequence struct {
actions []Action
}
// Build a reusable action pipeline before executing it against a Webview.
//
// sequence := webview.NewActionSequence().
// Navigate("https://example.com").
// WaitForSelector("form").
// Click("button")
func NewActionSequence() *ActionSequence {
return &ActionSequence{
actions: make([]Action, 0),
}
}
// Add adds an action to the sequence.
func (s *ActionSequence) Add(action Action) *ActionSequence {
s.actions = append(s.actions, action)
return s
}
// Click adds a click action.
func (s *ActionSequence) Click(selector string) *ActionSequence {
return s.Add(ClickAction{Selector: selector})
}
// Type adds a type action.
func (s *ActionSequence) Type(selector, text string) *ActionSequence {
return s.Add(TypeAction{Selector: selector, Text: text})
}
// Navigate adds a navigate action.
func (s *ActionSequence) Navigate(url string) *ActionSequence {
return s.Add(NavigateAction{URL: url})
}
// Wait adds a wait action.
func (s *ActionSequence) Wait(d time.Duration) *ActionSequence {
return s.Add(WaitAction{Duration: d})
}
// WaitForSelector adds a wait for selector action.
func (s *ActionSequence) WaitForSelector(selector string) *ActionSequence {
return s.Add(WaitForSelectorAction{Selector: selector})
}
// Scroll adds a scroll action.
func (s *ActionSequence) Scroll(x, y int) *ActionSequence {
return s.Add(ScrollAction{X: x, Y: y})
}
// ScrollIntoView adds a scroll-into-view action.
func (s *ActionSequence) ScrollIntoView(selector string) *ActionSequence {
return s.Add(ScrollIntoViewAction{Selector: selector})
}
// Focus adds a focus action.
func (s *ActionSequence) Focus(selector string) *ActionSequence {
return s.Add(FocusAction{Selector: selector})
}
// Blur adds a blur action.
func (s *ActionSequence) Blur(selector string) *ActionSequence {
return s.Add(BlurAction{Selector: selector})
}
// Clear adds a clear action.
func (s *ActionSequence) Clear(selector string) *ActionSequence {
return s.Add(ClearAction{Selector: selector})
}
// Select adds a select action.
func (s *ActionSequence) Select(selector, value string) *ActionSequence {
return s.Add(SelectAction{Selector: selector, Value: value})
}
// Check adds a check action.
func (s *ActionSequence) Check(selector string, checked bool) *ActionSequence {
return s.Add(CheckAction{Selector: selector, Checked: checked})
}
// Hover adds a hover action.
func (s *ActionSequence) Hover(selector string) *ActionSequence {
return s.Add(HoverAction{Selector: selector})
}
// DoubleClick adds a double-click action.
func (s *ActionSequence) DoubleClick(selector string) *ActionSequence {
return s.Add(DoubleClickAction{Selector: selector})
}
// RightClick adds a right-click action.
func (s *ActionSequence) RightClick(selector string) *ActionSequence {
return s.Add(RightClickAction{Selector: selector})
}
// PressKey adds a key press action.
func (s *ActionSequence) PressKey(key string) *ActionSequence {
return s.Add(PressKeyAction{Key: key})
}
// SetAttribute adds a set-attribute action.
func (s *ActionSequence) SetAttribute(selector, attribute, value string) *ActionSequence {
return s.Add(SetAttributeAction{Selector: selector, Attribute: attribute, Value: value})
}
// RemoveAttribute adds a remove-attribute action.
func (s *ActionSequence) RemoveAttribute(selector, attribute string) *ActionSequence {
return s.Add(RemoveAttributeAction{Selector: selector, Attribute: attribute})
}
// SetValue adds a set-value action.
func (s *ActionSequence) SetValue(selector, value string) *ActionSequence {
return s.Add(SetValueAction{Selector: selector, Value: value})
}
// UploadFile adds a file-upload action.
func (s *ActionSequence) UploadFile(selector string, filePaths []string) *ActionSequence {
return s.Add(UploadFileAction{
Selector: selector,
FilePaths: append([]string(nil), filePaths...),
})
}
// DragAndDrop adds a drag-and-drop action.
func (s *ActionSequence) DragAndDrop(sourceSelector, targetSelector string) *ActionSequence {
return s.Add(DragAndDropAction{
SourceSelector: sourceSelector,
TargetSelector: targetSelector,
})
}
// Execute executes all actions in the sequence.
func (s *ActionSequence) Execute(ctx context.Context, wv *Webview) error {
for i, action := range s.actions {
if err := action.Execute(ctx, wv); err != nil {
return coreerr.E("ActionSequence.Execute", core.Sprintf("action index %d failed", i), err)
}
}
return nil
}
// UploadFile uploads a file to a file input element.
func (wv *Webview) UploadFile(selector string, filePaths []string) error {
ctx, cancel := context.WithTimeout(wv.ctx, wv.timeout)
defer cancel()
return wv.uploadFile(ctx, selector, filePaths)
}
func (wv *Webview) uploadFile(ctx context.Context, selector string, filePaths []string) error {
// Get the element's node ID
elem, err := wv.querySelector(ctx, selector)
if err != nil {
return coreerr.E("Webview.UploadFile", "failed to find file input", err)
}
// Use DOM.setFileInputFiles to set the files
_, err = wv.client.Call(ctx, "DOM.setFileInputFiles", map[string]any{
"nodeId": elem.NodeID,
"files": filePaths,
})
if err != nil {
return coreerr.E("Webview.UploadFile", "failed to upload file", err)
}
return nil
}
// DragAndDrop performs a drag and drop operation.
func (wv *Webview) DragAndDrop(sourceSelector, targetSelector string) error {
ctx, cancel := context.WithTimeout(wv.ctx, wv.timeout)
defer cancel()
return wv.dragAndDrop(ctx, sourceSelector, targetSelector)
}
func (wv *Webview) dragAndDrop(ctx context.Context, sourceSelector, targetSelector string) error {
// Get source and target elements
source, err := wv.querySelector(ctx, sourceSelector)
if err != nil {
return coreerr.E("Webview.DragAndDrop", "source element not found", err)
}
if source.BoundingBox == nil {
return coreerr.E("Webview.DragAndDrop", "source element has no bounding box", nil)
}
target, err := wv.querySelector(ctx, targetSelector)
if err != nil {
return coreerr.E("Webview.DragAndDrop", "target element not found", err)
}
if target.BoundingBox == nil {
return coreerr.E("Webview.DragAndDrop", "target element has no bounding box", nil)
}
// Calculate center points
sourceX := source.BoundingBox.X + source.BoundingBox.Width/2
sourceY := source.BoundingBox.Y + source.BoundingBox.Height/2
targetX := target.BoundingBox.X + target.BoundingBox.Width/2
targetY := target.BoundingBox.Y + target.BoundingBox.Height/2
// Mouse down on source
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mousePressed",
"x": sourceX,
"y": sourceY,
"button": "left",
"clickCount": 1,
})
if err != nil {
return coreerr.E("Webview.DragAndDrop", "failed to press source element", err)
}
// Move to target
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseMoved",
"x": targetX,
"y": targetY,
"button": "left",
})
if err != nil {
return coreerr.E("Webview.DragAndDrop", "failed to move to target element", err)
}
// Mouse up on target
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseReleased",
"x": targetX,
"y": targetY,
"button": "left",
"clickCount": 1,
})
if err != nil {
return coreerr.E("Webview.DragAndDrop", "failed to release target element", err)
}
return nil
}