/
mouse_options.go
137 lines (120 loc) · 2.92 KB
/
mouse_options.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
package common
import (
"context"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/k6ext"
)
type MouseClickOptions struct {
Button string `json:"button"`
ClickCount int64 `json:"clickCount"`
Delay int64 `json:"delay"`
}
type MouseDblClickOptions struct {
Button string `json:"button"`
Delay int64 `json:"delay"`
}
type MouseDownUpOptions struct {
Button string `json:"button"`
ClickCount int64 `json:"clickCount"`
}
type MouseMoveOptions struct {
Steps int64 `json:"steps"`
}
func NewMouseClickOptions() *MouseClickOptions {
return &MouseClickOptions{
Button: "left",
ClickCount: 1,
Delay: 0,
}
}
func (o *MouseClickOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "button":
o.Button = opts.Get(k).String()
case "clickCount":
o.ClickCount = opts.Get(k).ToInteger()
case "delay":
o.Delay = opts.Get(k).ToInteger()
}
}
}
return nil
}
func (o *MouseClickOptions) ToMouseDownUpOptions() *MouseDownUpOptions {
o2 := NewMouseDownUpOptions()
o2.Button = o.Button
o2.ClickCount = o.ClickCount
return o2
}
func NewMouseDblClickOptions() *MouseDblClickOptions {
return &MouseDblClickOptions{
Button: "left",
Delay: 0,
}
}
func (o *MouseDblClickOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "button":
o.Button = opts.Get(k).String()
case "delay":
o.Delay = opts.Get(k).ToInteger()
}
}
}
return nil
}
// ToMouseClickOptions converts MouseDblClickOptions to a MouseClickOptions.
func (o *MouseDblClickOptions) ToMouseClickOptions() *MouseClickOptions {
o2 := NewMouseClickOptions()
o2.Button = o.Button
o2.ClickCount = 2
o2.Delay = o.Delay
return o2
}
func NewMouseDownUpOptions() *MouseDownUpOptions {
return &MouseDownUpOptions{
Button: "left",
ClickCount: 1,
}
}
func (o *MouseDownUpOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "button":
o.Button = opts.Get(k).String()
case "clickCount":
o.ClickCount = opts.Get(k).ToInteger()
}
}
}
return nil
}
func NewMouseMoveOptions() *MouseMoveOptions {
return &MouseMoveOptions{
Steps: 1,
}
}
func (o *MouseMoveOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "steps":
o.Steps = opts.Get(k).ToInteger()
}
}
}
return nil
}