/
element_handle_options.go
570 lines (506 loc) · 15.8 KB
/
element_handle_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
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
package common
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/k6ext"
)
type ElementHandleBaseOptions struct {
Force bool `json:"force"`
NoWaitAfter bool `json:"noWaitAfter"`
Timeout time.Duration `json:"timeout"`
}
type ElementHandleBasePointerOptions struct {
ElementHandleBaseOptions
Position *Position `json:"position"`
Trial bool `json:"trial"`
}
// ScrollPosition is a parameter for scrolling an element.
type ScrollPosition string
const (
// ScrollPositionStart scrolls an element at the top of its parent.
ScrollPositionStart ScrollPosition = "start"
// ScrollPositionCenter scrolls an element at the center of its parent.
ScrollPositionCenter ScrollPosition = "center"
// ScrollPositionEnd scrolls an element at the end of its parent.
ScrollPositionEnd ScrollPosition = "end"
// ScrollPositionNearest scrolls an element at the nearest position of its parent.
ScrollPositionNearest ScrollPosition = "nearest"
)
// ScrollIntoViewOptions change the behavior of ScrollIntoView.
// See: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
type ScrollIntoViewOptions struct {
// Block defines vertical alignment.
// One of start, center, end, or nearest.
// Defaults to start.
Block ScrollPosition `json:"block"`
// Inline defines horizontal alignment.
// One of start, center, end, or nearest.
// Defaults to nearest.
Inline ScrollPosition `json:"inline"`
}
type ElementHandleCheckOptions struct {
ElementHandleBasePointerOptions
}
type ElementHandleClickOptions struct {
ElementHandleBasePointerOptions
Button string `json:"button"`
ClickCount int64 `json:"clickCount"`
Delay int64 `json:"delay"`
Modifiers []string `json:"modifiers"`
}
type ElementHandleDblclickOptions struct {
ElementHandleBasePointerOptions
Button string `json:"button"`
Delay int64 `json:"delay"`
Modifiers []string `json:"modifiers"`
}
type ElementHandleHoverOptions struct {
ElementHandleBasePointerOptions
Modifiers []string `json:"modifiers"`
}
// File is the descriptor of a single file.
type File struct {
Name string `json:"name"`
Mimetype string `json:"mimeType"`
Buffer string `json:"buffer"`
}
// Files is the input parameter for ElementHandle.SetInputFiles.
type Files struct {
Payload []*File `json:"payload"`
}
// ElementHandleSetInputFilesOptions are options for ElementHandle.SetInputFiles.
type ElementHandleSetInputFilesOptions struct {
ElementHandleBaseOptions
}
type ElementHandlePressOptions struct {
Delay int64 `json:"delay"`
NoWaitAfter bool `json:"noWaitAfter"`
Timeout time.Duration `json:"timeout"`
}
type ElementHandleScreenshotOptions struct {
Path string `json:"path"`
Format ImageFormat `json:"format"`
OmitBackground bool `json:"omitBackground"`
Quality int64 `json:"quality"`
Timeout time.Duration `json:"timeout"`
}
type ElementHandleSetCheckedOptions struct {
ElementHandleBasePointerOptions
Strict bool `json:"strict"`
}
type ElementHandleTapOptions struct {
ElementHandleBasePointerOptions
Modifiers []string `json:"modifiers"`
}
type ElementHandleTypeOptions struct {
Delay int64 `json:"delay"`
NoWaitAfter bool `json:"noWaitAfter"`
Timeout time.Duration `json:"timeout"`
}
type ElementHandleWaitForElementStateOptions struct {
Timeout time.Duration `json:"timeout"`
}
func NewElementHandleBaseOptions(defaultTimeout time.Duration) *ElementHandleBaseOptions {
return &ElementHandleBaseOptions{
Force: false,
NoWaitAfter: false,
Timeout: defaultTimeout,
}
}
func (o *ElementHandleBaseOptions) Parse(ctx context.Context, opts goja.Value) error {
if !gojaValueExists(opts) {
return nil
}
gopts := opts.ToObject(k6ext.Runtime(ctx))
for _, k := range gopts.Keys() {
switch k {
case "force":
o.Force = gopts.Get(k).ToBoolean()
case "noWaitAfter": //nolint:goconst
o.NoWaitAfter = gopts.Get(k).ToBoolean()
case "timeout":
o.Timeout = time.Duration(gopts.Get(k).ToInteger()) * time.Millisecond
}
}
return nil
}
func NewElementHandleBasePointerOptions(defaultTimeout time.Duration) *ElementHandleBasePointerOptions {
return &ElementHandleBasePointerOptions{
ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout),
Position: nil,
Trial: false,
}
}
func (o *ElementHandleBasePointerOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil {
return err
}
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "position":
var p map[string]float64
o.Position = &Position{}
if rt.ExportTo(opts.Get(k), &p) != nil {
o.Position.X = p["x"]
o.Position.Y = p["y"]
}
case "trial":
o.Trial = opts.Get(k).ToBoolean()
}
}
}
return nil
}
func NewElementHandleCheckOptions(defaultTimeout time.Duration) *ElementHandleCheckOptions {
return &ElementHandleCheckOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
}
}
func (o *ElementHandleCheckOptions) Parse(ctx context.Context, opts goja.Value) error {
return o.ElementHandleBasePointerOptions.Parse(ctx, opts)
}
// NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption.
func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOptions {
return &ElementHandleSetInputFilesOptions{
ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout),
}
}
// addFile to the struct. Input value can only be a file descriptor object.
func (f *Files) addFile(ctx context.Context, file goja.Value) error {
if !gojaValueExists(file) {
return nil
}
rt := k6ext.Runtime(ctx)
fileType := file.ExportType()
switch fileType.Kind() { //nolint:exhaustive
case reflect.Map: // file descriptor object
var parsedFile File
if err := rt.ExportTo(file, &parsedFile); err != nil {
return fmt.Errorf("parsing file descriptor: %w", err)
}
f.Payload = append(f.Payload, &parsedFile)
default:
return fmt.Errorf("invalid parameter type : %s", fileType.Kind().String())
}
return nil
}
// Parse parses the Files struct from the given goja.Value.
func (f *Files) Parse(ctx context.Context, files goja.Value) error {
rt := k6ext.Runtime(ctx)
if !gojaValueExists(files) {
return nil
}
optsType := files.ExportType()
switch optsType.Kind() { //nolint:exhaustive
case reflect.Slice: // array of filePaths or array of file descriptor objects
gopts := files.ToObject(rt)
for _, k := range gopts.Keys() {
err := f.addFile(ctx, gopts.Get(k))
if err != nil {
return err
}
}
default: // filePath or file descriptor object
return f.addFile(ctx, files)
}
return nil
}
// Parse parses the ElementHandleSetInputFilesOption from the given opts.
func (o *ElementHandleSetInputFilesOptions) Parse(ctx context.Context, opts goja.Value) error {
if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil {
return err
}
return nil
}
func NewElementHandleClickOptions(defaultTimeout time.Duration) *ElementHandleClickOptions {
return &ElementHandleClickOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
Button: "left",
ClickCount: 1,
Delay: 0,
Modifiers: []string{},
}
}
func (o *ElementHandleClickOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
return err
}
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()
case "modifiers":
var m []string
if err := rt.ExportTo(opts.Get(k), &m); err != nil {
return err
}
o.Modifiers = m
}
}
}
return nil
}
func (o *ElementHandleClickOptions) ToMouseClickOptions() *MouseClickOptions {
o2 := NewMouseClickOptions()
o2.Button = o.Button
o2.ClickCount = o.ClickCount
o2.Delay = o.Delay
return o2
}
func NewElementHandleDblclickOptions(defaultTimeout time.Duration) *ElementHandleDblclickOptions {
return &ElementHandleDblclickOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
Button: "left",
Delay: 0,
Modifiers: []string{},
}
}
func (o *ElementHandleDblclickOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
return err
}
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()
case "modifiers":
var m []string
if err := rt.ExportTo(opts.Get(k), &m); err != nil {
return err
}
o.Modifiers = m
}
}
}
return nil
}
func (o *ElementHandleDblclickOptions) ToMouseClickOptions() *MouseClickOptions {
o2 := NewMouseClickOptions()
o2.Button = o.Button
o2.ClickCount = 2
o2.Delay = o.Delay
return o2
}
func NewElementHandleHoverOptions(defaultTimeout time.Duration) *ElementHandleHoverOptions {
return &ElementHandleHoverOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
Modifiers: []string{},
}
}
func (o *ElementHandleHoverOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
return err
}
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "modifiers":
var m []string
if err := rt.ExportTo(opts.Get(k), &m); err != nil {
return err
}
o.Modifiers = m
}
}
}
return nil
}
func NewElementHandlePressOptions(defaultTimeout time.Duration) *ElementHandlePressOptions {
return &ElementHandlePressOptions{
Delay: 0,
NoWaitAfter: false,
Timeout: defaultTimeout,
}
}
func (o *ElementHandlePressOptions) 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 "delay":
o.Delay = opts.Get(k).ToInteger()
case "noWaitAfter":
o.NoWaitAfter = opts.Get(k).ToBoolean()
case "timeout":
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
}
return nil
}
func (o *ElementHandlePressOptions) ToBaseOptions() *ElementHandleBaseOptions {
o2 := ElementHandleBaseOptions{}
o2.Force = false
o2.NoWaitAfter = o.NoWaitAfter
o2.Timeout = o.Timeout
return &o2
}
func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHandleScreenshotOptions {
return &ElementHandleScreenshotOptions{
Path: "",
Format: ImageFormatPNG,
OmitBackground: false,
Quality: 100,
Timeout: defaultTimeout,
}
}
func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
formatSpecified := false
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "omitBackground":
o.OmitBackground = opts.Get(k).ToBoolean()
case "path":
o.Path = opts.Get(k).String()
case "quality":
o.Quality = opts.Get(k).ToInteger()
case "type":
if f, ok := imageFormatToID[opts.Get(k).String()]; ok {
o.Format = f
formatSpecified = true
}
case "timeout":
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
// Infer file format by path if format not explicitly specified (default is PNG)
if o.Path != "" && !formatSpecified {
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
o.Format = ImageFormatJPEG
}
}
}
return nil
}
func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHandleSetCheckedOptions {
return &ElementHandleSetCheckedOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
Strict: false,
}
}
func (o *ElementHandleSetCheckedOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
return err
}
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "strict":
o.Strict = opts.Get(k).ToBoolean()
}
}
}
return nil
}
func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapOptions {
return &ElementHandleTapOptions{
ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout),
Modifiers: []string{},
}
}
func (o *ElementHandleTapOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
return err
}
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "modifiers":
var m []string
if err := rt.ExportTo(opts.Get(k), &m); err != nil {
return err
}
o.Modifiers = m
}
}
}
return nil
}
func NewElementHandleTypeOptions(defaultTimeout time.Duration) *ElementHandleTypeOptions {
return &ElementHandleTypeOptions{
Delay: 0,
NoWaitAfter: false,
Timeout: defaultTimeout,
}
}
func (o *ElementHandleTypeOptions) 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 "delay":
o.Delay = opts.Get(k).ToInteger()
case "noWaitAfter":
o.NoWaitAfter = opts.Get(k).ToBoolean()
case "timeout":
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
}
return nil
}
func (o *ElementHandleTypeOptions) ToBaseOptions() *ElementHandleBaseOptions {
o2 := ElementHandleBaseOptions{}
o2.Force = false
o2.NoWaitAfter = o.NoWaitAfter
o2.Timeout = o.Timeout
return &o2
}
func NewElementHandleWaitForElementStateOptions(defaultTimeout time.Duration) *ElementHandleWaitForElementStateOptions {
return &ElementHandleWaitForElementStateOptions{
Timeout: defaultTimeout,
}
}
func (o *ElementHandleWaitForElementStateOptions) 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 "timeout":
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
}
return nil
}
// ElementHandleDispatchEventOptions are options for ElementHandle.dispatchEvent.
type ElementHandleDispatchEventOptions struct {
*ElementHandleBaseOptions
}
// NewElementHandleDispatchEventOptions returns a new ElementHandleDispatchEventOptions.
func NewElementHandleDispatchEventOptions(defaultTimeout time.Duration) *ElementHandleDispatchEventOptions {
return &ElementHandleDispatchEventOptions{
ElementHandleBaseOptions: NewElementHandleBaseOptions(defaultTimeout),
}
}