This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathframe_mapping.go
315 lines (305 loc) · 10.6 KB
/
frame_mapping.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
package browser
import (
"fmt"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
)
// mapFrame to the JS module.
//
//nolint:funlen
func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
maps := mapping{
"check": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Check(selector, opts) //nolint:wrapcheck
})
},
"childFrames": func() []mapping {
var (
mcfs []mapping
cfs = f.ChildFrames()
)
for _, fr := range cfs {
mcfs = append(mcfs, mapFrame(vu, fr))
}
return mcfs
},
"click": func(selector string, opts goja.Value) (*goja.Promise, error) {
popts, err := parseFrameClickOptions(vu.Context(), opts, f.Timeout())
if err != nil {
return nil, err
}
return k6ext.Promise(vu.Context(), func() (any, error) {
err := f.Click(selector, popts)
return nil, err //nolint:wrapcheck
}), nil
},
"content": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.Content() //nolint:wrapcheck
})
},
"dblclick": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Dblclick(selector, opts) //nolint:wrapcheck
})
},
"dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) (*goja.Promise, error) {
popts := common.NewFrameDispatchEventOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame dispatch event options: %w", err)
}
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.DispatchEvent(selector, typ, exportArg(eventInit), popts) //nolint:wrapcheck
}), nil
},
"evaluate": func(pageFunction goja.Value, gargs ...goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.Evaluate(pageFunction.String(), exportArgs(gargs)...) //nolint:wrapcheck
})
},
"evaluateHandle": func(pageFunction goja.Value, gargs ...goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
jsh, err := f.EvaluateHandle(pageFunction.String(), exportArgs(gargs)...)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapJSHandle(vu, jsh), nil
})
},
"fill": func(selector, value string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Fill(selector, value, opts) //nolint:wrapcheck
})
},
"focus": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Focus(selector, opts) //nolint:wrapcheck
})
},
"frameElement": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
fe, err := f.FrameElement()
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapElementHandle(vu, fe), nil
})
},
"getAttribute": func(selector, name string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
s, ok, err := f.GetAttribute(selector, name, opts)
if err != nil {
return nil, err //nolint:wrapcheck
}
if !ok {
return nil, nil
}
return s, nil
})
},
"goto": func(url string, opts goja.Value) (*goja.Promise, error) {
gopts := common.NewFrameGotoOptions(
f.Referrer(),
f.NavigationTimeout(),
)
if err := gopts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame navigation options to %q: %w", url, err)
}
return k6ext.Promise(vu.Context(), func() (any, error) {
resp, err := f.Goto(url, gopts)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapResponse(vu, resp), nil
}), nil
},
"hover": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Hover(selector, opts) //nolint:wrapcheck
})
},
"innerHTML": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.InnerHTML(selector, opts) //nolint:wrapcheck
})
},
"innerText": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.InnerText(selector, opts) //nolint:wrapcheck
})
},
"inputValue": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.InputValue(selector, opts) //nolint:wrapcheck
})
},
"isChecked": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsChecked(selector, opts) //nolint:wrapcheck
})
},
"isDetached": f.IsDetached,
"isDisabled": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsDisabled(selector, opts) //nolint:wrapcheck
})
},
"isEditable": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsEditable(selector, opts) //nolint:wrapcheck
})
},
"isEnabled": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsEnabled(selector, opts) //nolint:wrapcheck
})
},
"isHidden": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsHidden(selector, opts) //nolint:wrapcheck
})
},
"isVisible": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.IsVisible(selector, opts) //nolint:wrapcheck
})
},
"locator": func(selector string, opts goja.Value) mapping {
return mapLocator(vu, f.Locator(selector, opts))
},
"name": f.Name,
"page": func() mapping {
return mapPage(vu, f.Page())
},
"parentFrame": func() mapping {
return mapFrame(vu, f.ParentFrame())
},
"press": func(selector, key string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Press(selector, key, opts) //nolint:wrapcheck
})
},
"selectOption": func(selector string, values goja.Value, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.SelectOption(selector, values, opts) //nolint:wrapcheck
})
},
"setContent": func(html string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.SetContent(html, opts) //nolint:wrapcheck
})
},
"setInputFiles": func(selector string, files goja.Value, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.SetInputFiles(selector, files, opts) //nolint:wrapcheck
})
},
"tap": func(selector string, opts goja.Value) (*goja.Promise, error) {
popts := common.NewFrameTapOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame tap options: %w", err)
}
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Tap(selector, popts) //nolint:wrapcheck
}), nil
},
"textContent": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.TextContent(selector, opts) //nolint:wrapcheck
})
},
"title": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return f.Title(), nil
})
},
"type": func(selector, text string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Type(selector, text, opts) //nolint:wrapcheck
})
},
"uncheck": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Uncheck(selector, opts) //nolint:wrapcheck
})
},
"url": f.URL,
"waitForFunction": func(pageFunc, opts goja.Value, args ...goja.Value) (*goja.Promise, error) {
js, popts, pargs, err := parseWaitForFunctionArgs(
vu.Context(), f.Timeout(), pageFunc, opts, args...,
)
if err != nil {
return nil, fmt.Errorf("frame waitForFunction: %w", err)
}
return k6ext.Promise(vu.Context(), func() (result any, reason error) {
return f.WaitForFunction(js, popts, pargs...) //nolint:wrapcheck
}), nil
},
"waitForLoadState": func(state string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.WaitForLoadState(state, opts) //nolint:wrapcheck
})
},
"waitForNavigation": func(opts goja.Value) (*goja.Promise, error) {
popts := common.NewFrameWaitForNavigationOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame wait for navigation options: %w", err)
}
return k6ext.Promise(vu.Context(), func() (result any, reason error) {
resp, err := f.WaitForNavigation(popts)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapResponse(vu, resp), nil
}), nil
},
"waitForSelector": func(selector string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
eh, err := f.WaitForSelector(selector, opts)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapElementHandle(vu, eh), nil
})
},
"waitForTimeout": func(timeout int64) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
f.WaitForTimeout(timeout)
return nil, nil
})
},
}
maps["$"] = func(selector string) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
eh, err := f.Query(selector, common.StrictModeOff)
if err != nil {
return nil, err //nolint:wrapcheck
}
// ElementHandle can be null when the selector does not match any elements.
// We do not want to map nil elementHandles since the expectation is a
// null result in the test script for this case.
if eh == nil {
return nil, nil
}
ehm := mapElementHandle(vu, eh)
return ehm, nil
})
}
maps["$$"] = func(selector string) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
ehs, err := f.QueryAll(selector)
if err != nil {
return nil, err //nolint:wrapcheck
}
var mehs []mapping
for _, eh := range ehs {
ehm := mapElementHandle(vu, eh)
mehs = append(mehs, ehm)
}
return mehs, nil
})
}
return maps
}