-
Notifications
You must be signed in to change notification settings - Fork 42
/
browser_context_options.go
262 lines (237 loc) · 7.55 KB
/
browser_context_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
package common
import (
"context"
"errors"
"fmt"
"time"
"github.com/grafana/sobek"
"github.com/grafana/xk6-browser/k6ext"
)
// Geolocation represents a geolocation.
type Geolocation struct {
Latitude float64 `js:"latitude"`
Longitude float64 `js:"longitude"`
Accurracy float64 `js:"accurracy"`
}
// NewGeolocation creates a new instance of Geolocation.
func NewGeolocation() *Geolocation {
return &Geolocation{}
}
// Parse parses the geolocation options.
func (g *Geolocation) Parse(ctx context.Context, opts sobek.Value) error { //nolint:cyclop
rt := k6ext.Runtime(ctx)
longitude := 0.0
latitude := 0.0
accuracy := 0.0
if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "accuracy":
accuracy = opts.Get(k).ToFloat()
case "latitude":
latitude = opts.Get(k).ToFloat()
case "longitude":
longitude = opts.Get(k).ToFloat()
}
}
}
if longitude < -180 || longitude > 180 {
return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, longitude)
}
if latitude < -90 || latitude > 90 {
return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, latitude)
}
if accuracy < 0 {
return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, accuracy)
}
g.Accurracy = accuracy
g.Latitude = latitude
g.Longitude = longitude
return nil
}
// BrowserContextOptions stores browser context options.
type BrowserContextOptions struct {
AcceptDownloads bool `js:"acceptDownloads"`
BypassCSP bool `js:"bypassCSP"`
ColorScheme ColorScheme `js:"colorScheme"`
DeviceScaleFactor float64 `js:"deviceScaleFactor"`
ExtraHTTPHeaders map[string]string `js:"extraHTTPHeaders"`
Geolocation *Geolocation `js:"geolocation"`
HasTouch bool `js:"hasTouch"`
HttpCredentials *Credentials `js:"httpCredentials"`
IgnoreHTTPSErrors bool `js:"ignoreHTTPSErrors"`
IsMobile bool `js:"isMobile"`
JavaScriptEnabled bool `js:"javaScriptEnabled"`
Locale string `js:"locale"`
Offline bool `js:"offline"`
Permissions []string `js:"permissions"`
ReducedMotion ReducedMotion `js:"reducedMotion"`
Screen *Screen `js:"screen"`
TimezoneID string `js:"timezoneID"`
UserAgent string `js:"userAgent"`
VideosPath string `js:"videosPath"`
Viewport *Viewport `js:"viewport"`
}
// NewBrowserContextOptions creates a default set of browser context options.
func NewBrowserContextOptions() *BrowserContextOptions {
return &BrowserContextOptions{
ColorScheme: ColorSchemeLight,
DeviceScaleFactor: 1.0,
ExtraHTTPHeaders: make(map[string]string),
JavaScriptEnabled: true,
Locale: DefaultLocale,
Permissions: []string{},
ReducedMotion: ReducedMotionNoPreference,
Screen: &Screen{Width: DefaultScreenWidth, Height: DefaultScreenHeight},
Viewport: &Viewport{Width: DefaultScreenWidth, Height: DefaultScreenHeight},
}
}
// Parse parses the browser context options.
func (b *BrowserContextOptions) Parse(ctx context.Context, opts sobek.Value) error { //nolint:cyclop,funlen,gocognit
rt := k6ext.Runtime(ctx)
if !sobekValueExists(opts) {
return nil
}
o := opts.ToObject(rt)
for _, k := range o.Keys() {
switch k {
case "acceptDownloads":
b.AcceptDownloads = o.Get(k).ToBoolean()
case "bypassCSP":
b.BypassCSP = o.Get(k).ToBoolean()
case "colorScheme":
switch ColorScheme(o.Get(k).String()) { //nolint:exhaustive
case "light":
b.ColorScheme = ColorSchemeLight
case "dark":
b.ColorScheme = ColorSchemeDark
default:
b.ColorScheme = ColorSchemeNoPreference
}
case "deviceScaleFactor":
b.DeviceScaleFactor = o.Get(k).ToFloat()
case "extraHTTPHeaders":
headers := o.Get(k).ToObject(rt)
for _, k := range headers.Keys() {
b.ExtraHTTPHeaders[k] = headers.Get(k).String()
}
case "geolocation":
geolocation := NewGeolocation()
if err := geolocation.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
return err
}
b.Geolocation = geolocation
case "hasTouch":
b.HasTouch = o.Get(k).ToBoolean()
case "httpCredentials":
credentials := NewCredentials()
if err := credentials.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
return err
}
b.HttpCredentials = credentials
case "ignoreHTTPSErrors":
b.IgnoreHTTPSErrors = o.Get(k).ToBoolean()
case "isMobile":
b.IsMobile = o.Get(k).ToBoolean()
case "javaScriptEnabled":
b.JavaScriptEnabled = o.Get(k).ToBoolean()
case "locale":
b.Locale = o.Get(k).String()
case "offline":
b.Offline = o.Get(k).ToBoolean()
case "permissions":
if ps, ok := o.Get(k).Export().([]any); ok {
for _, p := range ps {
b.Permissions = append(b.Permissions, fmt.Sprintf("%v", p))
}
}
case "reducedMotion":
switch ReducedMotion(o.Get(k).String()) { //nolint:exhaustive
case "reduce":
b.ReducedMotion = ReducedMotionReduce
default:
b.ReducedMotion = ReducedMotionNoPreference
}
case "screen":
screen := &Screen{}
if err := screen.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
return err
}
b.Screen = screen
case "timezoneID":
b.TimezoneID = o.Get(k).String()
case "userAgent":
b.UserAgent = o.Get(k).String()
case "viewport":
viewport := &Viewport{}
if err := viewport.Parse(ctx, o.Get(k).ToObject(rt)); err != nil {
return err
}
b.Viewport = viewport
}
}
return nil
}
// WaitForEventOptions are the options used by the browserContext.waitForEvent API.
type WaitForEventOptions struct {
Timeout time.Duration
PredicateFn sobek.Callable
}
// NewWaitForEventOptions created a new instance of WaitForEventOptions with a
// default timeout.
func NewWaitForEventOptions(defaultTimeout time.Duration) *WaitForEventOptions {
return &WaitForEventOptions{
Timeout: defaultTimeout,
}
}
// Parse will parse the options or a callable predicate function. It can parse
// only a callable predicate function or an object which contains a callable
// predicate function and a timeout.
func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate sobek.Value) error {
if !sobekValueExists(optsOrPredicate) {
return nil
}
var (
isCallable bool
rt = k6ext.Runtime(ctx)
)
w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate)
if isCallable {
return nil
}
opts := optsOrPredicate.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "predicate":
w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k))
if !isCallable {
return errors.New("predicate function is not callable")
}
case "timeout": //nolint:goconst
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}
return nil
}
// GrantPermissionsOptions is used by BrowserContext.GrantPermissions.
type GrantPermissionsOptions struct {
Origin string
}
// NewGrantPermissionsOptions returns a new GrantPermissionsOptions.
func NewGrantPermissionsOptions() *GrantPermissionsOptions {
return &GrantPermissionsOptions{}
}
// Parse parses the options from opts if opts exists in the sobek runtime.
func (g *GrantPermissionsOptions) Parse(ctx context.Context, opts sobek.Value) {
rt := k6ext.Runtime(ctx)
if sobekValueExists(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
if k == "origin" {
g.Origin = opts.Get(k).String()
break
}
}
}
}