-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpage_options.go
202 lines (183 loc) · 5.32 KB
/
page_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
package common
import (
"context"
"fmt"
"strings"
"time"
"github.com/chromedp/cdproto/page"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/k6ext"
)
type PageEmulateMediaOptions struct {
ColorScheme ColorScheme `json:"colorScheme"`
Media MediaType `json:"media"`
ReducedMotion ReducedMotion `json:"reducedMotion"`
}
type PageReloadOptions struct {
WaitUntil LifecycleEvent `json:"waitUntil" js:"waitUntil"`
Timeout time.Duration `json:"timeout"`
}
type PageScreenshotOptions struct {
Clip *page.Viewport `json:"clip"`
Path string `json:"path"`
Format ImageFormat `json:"format"`
FullPage bool `json:"fullPage"`
OmitBackground bool `json:"omitBackground"`
Quality int64 `json:"quality"`
}
type VideoCaptureOptions struct {
Path string `json:"path"`
Format VideoFormat `json:"format"`
FrameRate int64 `json:"frameRate"`
Quality int64 `json:"quality"`
EveryNthFrame int64 `json:"everyNthFrame"`
MaxWidth int64 `json:"maxWidth"`
MaxHeight int64 `json:"maxHeight"`
}
func NewPageEmulateMediaOptions(defaultMedia MediaType, defaultColorScheme ColorScheme, defaultReducedMotion ReducedMotion) *PageEmulateMediaOptions {
return &PageEmulateMediaOptions{
ColorScheme: defaultColorScheme,
Media: defaultMedia,
ReducedMotion: defaultReducedMotion,
}
}
func (o *PageEmulateMediaOptions) 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 "colorScheme":
o.ColorScheme = ColorScheme(opts.Get(k).String())
case "media":
o.Media = MediaType(opts.Get(k).String())
case "reducedMotion":
o.ReducedMotion = ReducedMotion(opts.Get(k).String())
}
}
}
return nil
}
func NewPageReloadOptions(defaultWaitUntil LifecycleEvent, defaultTimeout time.Duration) *PageReloadOptions {
return &PageReloadOptions{
WaitUntil: defaultWaitUntil,
Timeout: defaultTimeout,
}
}
func (o *PageReloadOptions) 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 "waitUntil":
lifeCycle := opts.Get(k).String()
if l, ok := lifecycleEventToID[lifeCycle]; ok {
o.WaitUntil = l
} else {
return fmt.Errorf("%q is not a valid lifecycle", lifeCycle)
}
case "timeout":
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
}
return nil
}
func NewPageScreenshotOptions() *PageScreenshotOptions {
return &PageScreenshotOptions{
Clip: nil,
Path: "",
Format: ImageFormatPNG,
FullPage: false,
OmitBackground: false,
Quality: 100,
}
}
func (o *PageScreenshotOptions) 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 "clip":
var c map[string]float64
if rt.ExportTo(opts.Get(k), &c) != nil {
o.Clip = &page.Viewport{
X: c["x"],
Y: c["y"],
Width: c["width"],
Height: c["height"],
Scale: 1,
}
}
case "fullPage":
o.FullPage = opts.Get(k).ToBoolean()
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
}
}
}
// Infer file format by path if format not explicitly specified (default is jpg)
if o.Path != "" && !formatSpecified {
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
o.Format = ImageFormatJPEG
}
}
}
return nil
}
func (o *VideoCaptureOptions) 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 "everyNthFrame":
o.EveryNthFrame = opts.Get(k).ToInteger()
case "frameRate":
o.FrameRate = opts.Get(k).ToInteger()
case "maxHeigth":
o.MaxHeight = opts.Get(k).ToInteger()
case "maxWidth":
o.MaxWidth = opts.Get(k).ToInteger()
case "path":
o.Path = opts.Get(k).String()
case "quality":
o.Quality = opts.Get(k).ToInteger()
case "format":
if f, ok := videoFormatToID[opts.Get(k).String()]; ok {
o.Format = f
formatSpecified = true
}
}
}
// Infer file format by path if format not explicitly specified (default is webm)
// TODO: throw error if format is not defined
if o.Path != "" && !formatSpecified {
if strings.HasSuffix(o.Path, ".webm") {
o.Format = VideoFormatWebM
}
}
}
return nil
}
func NewVidepCaptureOptions() *VideoCaptureOptions {
return &VideoCaptureOptions{
Path: "",
Format: VideoFormatWebM,
Quality: 100,
FrameRate: 25,
EveryNthFrame: 1,
}
}