-
Notifications
You must be signed in to change notification settings - Fork 42
/
mouse.go
187 lines (167 loc) · 5.32 KB
/
mouse.go
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
package common
import (
"context"
"fmt"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/input"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/k6ext"
)
// Mouse represents a mouse input device.
type Mouse struct {
ctx context.Context
session session
frame *Frame
timeoutSettings *TimeoutSettings
keyboard *Keyboard
x float64
y float64
button input.MouseButton
}
// NewMouse creates a new mouse.
func NewMouse(ctx context.Context, s session, f *Frame, ts *TimeoutSettings, k *Keyboard) *Mouse {
return &Mouse{
ctx: ctx,
session: s,
frame: f,
timeoutSettings: ts,
keyboard: k,
button: input.None,
}
}
func (m *Mouse) click(x float64, y float64, opts *MouseClickOptions) error {
mouseDownUpOpts := opts.ToMouseDownUpOptions()
if err := m.move(x, y, NewMouseMoveOptions()); err != nil {
return err
}
for i := 0; i < int(mouseDownUpOpts.ClickCount); i++ {
if err := m.down(mouseDownUpOpts); err != nil {
return err
}
if opts.Delay != 0 {
t := time.NewTimer(time.Duration(opts.Delay) * time.Millisecond)
select {
case <-m.ctx.Done():
t.Stop()
case <-t.C:
}
}
if err := m.up(mouseDownUpOpts); err != nil {
return err
}
}
return nil
}
func (m *Mouse) down(opts *MouseDownUpOptions) error {
m.button = input.MouseButton(opts.Button)
action := input.DispatchMouseEvent(input.MousePressed, m.x, m.y).
WithButton(input.MouseButton(opts.Button)).
WithModifiers(input.Modifier(m.keyboard.modifiers)).
WithClickCount(opts.ClickCount)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("mouse down: %w", err)
}
return nil
}
func (m *Mouse) move(x float64, y float64, opts *MouseMoveOptions) error {
fromX := m.x
fromY := m.y
m.x = x
m.y = y
for i := int64(1); i <= opts.Steps; i++ {
x := fromX + (m.x-fromX)*float64(i/opts.Steps)
y := fromY + (m.y-fromY)*float64(i/opts.Steps)
action := input.DispatchMouseEvent(input.MouseMoved, x, y).
WithButton(m.button).
WithModifiers(input.Modifier(m.keyboard.modifiers))
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("mouse move: %w", err)
}
}
return nil
}
func (m *Mouse) up(opts *MouseDownUpOptions) error {
m.button = input.None
action := input.DispatchMouseEvent(input.MouseReleased, m.x, m.y).
WithButton(input.MouseButton(opts.Button)).
WithModifiers(input.Modifier(m.keyboard.modifiers)).
WithClickCount(opts.ClickCount)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("mouse up: %w", err)
}
return nil
}
// Click will trigger a series of MouseMove, MouseDown and MouseUp events in the browser.
func (m *Mouse) Click(x float64, y float64, opts goja.Value) {
mouseOpts := NewMouseClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
k6ext.Panic(m.ctx, "parsing mouse click options: %w", err)
}
if err := m.click(x, y, mouseOpts); err != nil {
k6ext.Panic(m.ctx, "clicking on x:%f y:%f: %w", x, y, err)
}
}
// DblClick will trigger Click twice in quick succession.
func (m *Mouse) DblClick(x float64, y float64, opts goja.Value) {
mouseOpts := NewMouseDblClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
k6ext.Panic(m.ctx, "parsing double click options: %w", err)
}
if err := m.click(x, y, mouseOpts.ToMouseClickOptions()); err != nil {
k6ext.Panic(m.ctx, "double clicking on x:%f y:%f: %w", x, y, err)
}
}
// Down will trigger a MouseDown event in the browser.
func (m *Mouse) Down(opts goja.Value) {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
k6ext.Panic(m.ctx, "parsing mouse down options: %w", err)
}
if err := m.down(mouseOpts); err != nil {
k6ext.Panic(m.ctx, "pressing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}
}
// Move will trigger a MouseMoved event in the browser.
func (m *Mouse) Move(x float64, y float64, opts goja.Value) {
mouseOpts := NewMouseMoveOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
k6ext.Panic(m.ctx, "parsing mouse move options: %w", err)
}
if err := m.move(x, y, mouseOpts); err != nil {
k6ext.Panic(m.ctx, "moving the mouse pointer to x:%f y:%f: %w", x, y, err)
}
}
// Up will trigger a MouseUp event in the browser.
func (m *Mouse) Up(opts goja.Value) {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
k6ext.Panic(m.ctx, "parsing mouse up options: %w", err)
}
if err := m.up(mouseOpts); err != nil {
k6ext.Panic(m.ctx, "releasing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}
}
// Wheel will trigger a MouseWheel event in the browser
/*func (m *Mouse) Wheel(opts goja.Value) {
var deltaX float64 = 0.0
var deltaY float64 = 0.0
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "deltaX":
deltaX = opts.Get(k).ToFloat()
case "deltaY":
deltaY = opts.Get(k).ToFloat()
}
}
}
action := input.DispatchMouseEvent(input.MouseWheel, m.x, m.y).
WithModifiers(input.Modifier(m.keyboard.modifiers)).
WithDeltaX(deltaX).
WithDeltaY(deltaY)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
k6Throw(m.ctx, "mouse down: %w", err)
}
}*/