@@ -22,7 +22,6 @@ package common
22
22
23
23
import (
24
24
"context"
25
- "errors"
26
25
"fmt"
27
26
"reflect"
28
27
"time"
@@ -73,8 +72,7 @@ func NewBrowserContext(ctx context.Context, conn *Connection, browser *Browser,
73
72
}
74
73
75
74
func (b * BrowserContext ) AddCookies (cookies goja.Value ) {
76
- rt := k6common .GetRuntime (b .ctx )
77
- k6common .Throw (rt , errors .New ("BrowserContext.addCookies(cookies) has not been implemented yet" ))
75
+ k6Throw (b .ctx , "BrowserContext.addCookies(cookies) has not been implemented yet" )
78
76
}
79
77
80
78
// AddInitScript adds a script that will be initialized on all new pages.
@@ -120,53 +118,46 @@ func (b *BrowserContext) Browser() api.Browser {
120
118
121
119
// ClearCookies clears cookies.
122
120
func (b * BrowserContext ) ClearCookies () {
123
- rt := k6common .GetRuntime (b .ctx )
124
121
action := storage .ClearCookies ().WithBrowserContextID (b .id )
125
122
if err := action .Do (b .ctx ); err != nil {
126
- k6common . Throw ( rt , fmt . Errorf ( "unable to clear cookies permissions: %w" , err ) )
123
+ k6Throw ( b . ctx , "unable to clear cookies permissions: %w" , err )
127
124
}
128
125
}
129
126
130
127
// ClearPermissions clears any permission overrides.
131
128
func (b * BrowserContext ) ClearPermissions () {
132
- rt := k6common .GetRuntime (b .ctx )
133
129
action := cdpbrowser .ResetPermissions ().WithBrowserContextID (b .id )
134
130
if err := action .Do (b .ctx ); err != nil {
135
- k6common . Throw ( rt , fmt . Errorf ( "unable to clear override permissions: %w" , err ) )
131
+ k6Throw ( b . ctx , "unable to clear override permissions: %w" , err )
136
132
}
137
133
}
138
134
139
135
// Close shuts down the browser context.
140
136
func (b * BrowserContext ) Close () {
141
- rt := k6common .GetRuntime (b .ctx )
142
137
if b .id == "" {
143
- k6common . Throw ( rt , errors . New ( "default browser context can't be closed" ) )
138
+ k6Throw ( b . ctx , "default browser context can't be closed" )
144
139
}
145
140
if err := b .browser .disposeContext (b .id ); err != nil {
146
- k6common . Throw ( rt , err )
141
+ k6Throw ( b . ctx , "cannot dispose browser context: %w" , err )
147
142
}
148
143
}
149
144
150
145
func (b * BrowserContext ) Cookies () []goja.Object {
151
- rt := k6common .GetRuntime (b .ctx )
152
- k6common .Throw (rt , errors .New ("BrowserContext.cookies() has not been implemented yet" ))
146
+ k6Throw (b .ctx , "BrowserContext.cookies() has not been implemented yet" )
153
147
return nil
154
148
}
155
149
156
150
func (b * BrowserContext ) ExposeBinding (name string , callback goja.Callable , opts goja.Value ) {
157
- rt := k6common .GetRuntime (b .ctx )
158
- k6common .Throw (rt , errors .New ("BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet" ))
151
+ k6Throw (b .ctx , "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet" )
159
152
}
160
153
161
154
func (b * BrowserContext ) ExposeFunction (name string , callback goja.Callable ) {
162
- rt := k6common .GetRuntime (b .ctx )
163
- k6common .Throw (rt , errors .New ("BrowserContext.newCDPSession(name, callback) has not been implemented yet" ))
155
+ k6Throw (b .ctx , "BrowserContext.newCDPSession(name, callback) has not been implemented yet" )
164
156
}
165
157
166
158
// GrantPermissions enables the specified permissions, all others will be disabled.
167
159
func (b * BrowserContext ) GrantPermissions (permissions []string , opts goja.Value ) {
168
160
b .logger .Debugf ("BrowserContext:GrantPermissions" , "" )
169
- rt := k6common .GetRuntime (b .ctx )
170
161
permsToProtocol := map [string ]cdpbrowser.PermissionType {
171
162
"geolocation" : cdpbrowser .PermissionTypeGeolocation ,
172
163
"midi" : cdpbrowser .PermissionTypeMidi ,
@@ -186,6 +177,7 @@ func (b *BrowserContext) GrantPermissions(permissions []string, opts goja.Value)
186
177
}
187
178
origin := ""
188
179
180
+ rt := k6common .GetRuntime (b .ctx )
189
181
if opts != nil && ! goja .IsUndefined (opts ) && ! goja .IsNull (opts ) {
190
182
opts := opts .ToObject (rt )
191
183
for _ , k := range opts .Keys () {
@@ -203,25 +195,24 @@ func (b *BrowserContext) GrantPermissions(permissions []string, opts goja.Value)
203
195
204
196
action := cdpbrowser .GrantPermissions (perms ).WithOrigin (origin ).WithBrowserContextID (b .id )
205
197
if err := action .Do (b .ctx ); err != nil {
206
- k6common . Throw ( rt , fmt . Errorf ( "unable to override permissions: %w" , err ) )
198
+ k6Throw ( b . ctx , "unable to override permissions: %w" , err )
207
199
}
208
200
}
209
201
210
202
// NewCDPSession returns a new CDP session attached to this target
211
203
func (b * BrowserContext ) NewCDPSession () api.CDPSession {
212
- rt := k6common .GetRuntime (b .ctx )
213
- k6common .Throw (rt , errors .New ("BrowserContext.newCDPSession() has not been implemented yet" ))
204
+ k6Throw (b .ctx , "BrowserContext.newCDPSession() has not been implemented yet" )
214
205
return nil
215
206
}
216
207
217
208
// NewPage creates a new page inside this browser context.
218
209
func (b * BrowserContext ) NewPage () api.Page {
219
- b .logger .Debugf ("BrowserContext:NewPage" , "b.id :%v" , b .id )
210
+ b .logger .Debugf ("BrowserContext:NewPage" , "bid :%v" , b .id )
220
211
p , err := b .browser .newPageInContext (b .id )
221
212
if err != nil {
222
- rt := k6common .GetRuntime (b .ctx )
223
- k6common .Throw (rt , err )
213
+ k6Throw (b .ctx , "cannot create a new page: %w" , err )
224
214
}
215
+ b .logger .Debugf ("BrowserContext:NewPage:returns" , "bid:%v ptid:%s" , b .id , p .targetID )
225
216
return p
226
217
}
227
218
@@ -235,8 +226,7 @@ func (b *BrowserContext) Pages() []api.Page {
235
226
}
236
227
237
228
func (b * BrowserContext ) Route (url goja.Value , handler goja.Callable ) {
238
- rt := k6common .GetRuntime (b .ctx )
239
- k6common .Throw (rt , errors .New ("BrowserContext.setHTTPCredentials(httpCredentials) has not been implemented yet" ))
229
+ k6Throw (b .ctx , "BrowserContext.setHTTPCredentials(httpCredentials) has not been implemented yet" )
240
230
}
241
231
242
232
// SetDefaultNavigationTimeout sets the default navigation timeout in milliseconds.
@@ -250,32 +240,29 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) {
250
240
}
251
241
252
242
func (b * BrowserContext ) SetExtraHTTPHeaders (headers map [string ]string ) {
253
- rt := k6common .GetRuntime (b .ctx )
254
- k6common .Throw (rt , errors .New ("BrowserContext.setHTTPCredentials(httpCredentials) has not been implemented yet" ))
243
+ k6Throw (b .ctx , "BrowserContext.setHTTPCredentials(httpCredentials) has not been implemented yet" )
255
244
}
256
245
257
246
// SetGeolocation overrides the geo location of the user.
258
247
func (b * BrowserContext ) SetGeolocation (geolocation goja.Value ) {
259
- rt := k6common .GetRuntime (b .ctx )
260
248
g := NewGeolocation ()
261
249
if err := g .Parse (b .ctx , geolocation ); err != nil {
262
- k6common . Throw ( rt , err )
250
+ k6Throw ( b . ctx , "cannot parse geo location: %w" , err )
263
251
}
264
252
265
253
b .opts .Geolocation = g
266
254
for _ , p := range b .browser .getPages () {
267
255
if err := p .updateGeolocation (); err != nil {
268
- k6common . Throw ( rt , err )
256
+ k6Throw ( b . ctx , "cannot update geo location in target (%s): %w" , p . targetID , err )
269
257
}
270
258
}
271
259
}
272
260
273
261
// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.
274
262
func (b * BrowserContext ) SetHTTPCredentials (httpCredentials goja.Value ) {
275
- rt := k6common .GetRuntime (b .ctx )
276
263
c := NewCredentials ()
277
264
if err := c .Parse (b .ctx , httpCredentials ); err != nil {
278
- k6common . Throw ( rt , err )
265
+ k6Throw ( b . ctx , "cannot set HTTP credentials: %w" , err )
279
266
}
280
267
281
268
b .opts .HttpCredentials = c
@@ -293,13 +280,11 @@ func (b *BrowserContext) SetOffline(offline bool) {
293
280
}
294
281
295
282
func (b * BrowserContext ) StorageState (opts goja.Value ) {
296
- rt := k6common .GetRuntime (b .ctx )
297
- k6common .Throw (rt , errors .New ("BrowserContext.storageState(opts) has not been implemented yet" ))
283
+ k6Throw (b .ctx , "BrowserContext.storageState(opts) has not been implemented yet" )
298
284
}
299
285
300
286
func (b * BrowserContext ) Unroute (url goja.Value , handler goja.Callable ) {
301
- rt := k6common .GetRuntime (b .ctx )
302
- k6common .Throw (rt , errors .New ("BrowserContext.unroute(url, handler) has not been implemented yet" ))
287
+ k6Throw (b .ctx , "BrowserContext.unroute(url, handler) has not been implemented yet" )
303
288
}
304
289
305
290
func (b * BrowserContext ) WaitForEvent (event string , optsOrPredicate goja.Value ) interface {} {
@@ -321,7 +306,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
321
306
case "predicate" :
322
307
predicateFn , isCallable = goja .AssertFunction (opts .Get (k ))
323
308
if ! isCallable {
324
- k6common . Throw ( rt , errors . New ( "expected callable predicate" ) )
309
+ k6Throw ( b . ctx , "expected callable predicate" )
325
310
}
326
311
case "timeout" :
327
312
timeout = time .Duration (opts .Get (k ).ToInteger ()) * time .Millisecond
@@ -330,7 +315,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
330
315
default :
331
316
predicateFn , isCallable = goja .AssertFunction (optsOrPredicate )
332
317
if ! isCallable {
333
- k6common . Throw ( rt , errors . New ( "expected callable predicate" ) )
318
+ k6Throw ( b . ctx , "expected callable predicate" )
334
319
}
335
320
}
336
321
}
0 commit comments