@@ -117,8 +117,8 @@ type Frame struct {
117
117
118
118
documentHandle * ElementHandle
119
119
120
- mainExecutionContext * ExecutionContext
121
- utilityExecutionContext * ExecutionContext
120
+ mainExecutionContext frameExecutionContext
121
+ utilityExecutionContext frameExecutionContext
122
122
mainExecutionContextCh chan bool
123
123
utilityExecutionContextCh chan bool
124
124
mainExecutionContextHasWaited int32
@@ -311,11 +311,17 @@ func (f *Frame) document() (*ElementHandle, error) {
311
311
if f .documentHandle != nil {
312
312
return f .documentHandle , nil
313
313
}
314
+
314
315
f .waitForExecutionContext ("main" )
316
+
315
317
result , err := f .mainExecutionContext .evaluate (f .ctx , false , false , rt .ToValue ("document" ), nil )
316
318
if err != nil {
317
- return nil , err
319
+ return nil , fmt .Errorf ("frame document: cannot evaluate in main execution context: %w" , err )
320
+ }
321
+ if result == nil {
322
+ return nil , errors .New ("frame document: evaluate result is nil in main execution context" )
318
323
}
324
+
319
325
f .documentHandle = result .(* ElementHandle )
320
326
return f .documentHandle , err
321
327
}
@@ -350,10 +356,10 @@ func (f *Frame) navigated(name string, url string, loaderID string) {
350
356
}
351
357
352
358
func (f * Frame ) nullContext (execCtxID runtime.ExecutionContextID ) {
353
- if f .mainExecutionContext != nil && f .mainExecutionContext .id == execCtxID {
359
+ if f .mainExecutionContext != nil && f .mainExecutionContext .ID () == execCtxID {
354
360
f .mainExecutionContext = nil
355
361
f .documentHandle = nil
356
- } else if f .utilityExecutionContext != nil && f .utilityExecutionContext .id == execCtxID {
362
+ } else if f .utilityExecutionContext != nil && f .utilityExecutionContext .ID () == execCtxID {
357
363
f .utilityExecutionContext = nil
358
364
}
359
365
}
@@ -412,7 +418,7 @@ func (f *Frame) requestByID(reqID network.RequestID) *Request {
412
418
return frameSession .networkManager .requestFromID (reqID )
413
419
}
414
420
415
- func (f * Frame ) setContext (world string , execCtx * ExecutionContext ) {
421
+ func (f * Frame ) setContext (world string , execCtx frameExecutionContext ) {
416
422
if world == "main" {
417
423
f .mainExecutionContext = execCtx
418
424
if len (f .mainExecutionContextCh ) == 0 {
@@ -507,13 +513,13 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
507
513
508
514
func (f * Frame ) AddScriptTag (opts goja.Value ) {
509
515
rt := k6common .GetRuntime (f .ctx )
510
- k6common .Throw (rt , errors .New ("Frame.AddScriptTag() has not been implemented yet! " ))
516
+ k6common .Throw (rt , errors .New ("Frame.AddScriptTag() has not been implemented yet" ))
511
517
applySlowMo (f .ctx )
512
518
}
513
519
514
520
func (f * Frame ) AddStyleTag (opts goja.Value ) {
515
521
rt := k6common .GetRuntime (f .ctx )
516
- k6common .Throw (rt , errors .New ("Frame.AddStyleTag() has not been implemented yet! " ))
522
+ k6common .Throw (rt , errors .New ("Frame.AddStyleTag() has not been implemented yet" ))
517
523
applySlowMo (f .ctx )
518
524
}
519
525
@@ -1244,3 +1250,46 @@ func (f *Frame) WaitForTimeout(timeout int64) {
1244
1250
case <- time .After (time .Duration (timeout ) * time .Millisecond ):
1245
1251
}
1246
1252
}
1253
+
1254
+ // frameExecutionContext represents a JS execution context that belongs to Frame.
1255
+ type frameExecutionContext interface {
1256
+ // adoptBackendNodeId adopts specified backend node into this execution
1257
+ // context from another execution context.
1258
+ adoptBackendNodeId (backendNodeID cdp.BackendNodeID ) (* ElementHandle , error )
1259
+
1260
+ // adoptElementHandle adopts the specified element handle into this
1261
+ // execution context from another execution context.
1262
+ adoptElementHandle (elementHandle * ElementHandle ) (* ElementHandle , error )
1263
+
1264
+ // evaluate will evaluate provided callable within this execution
1265
+ // context and return by value or handle.
1266
+ evaluate (
1267
+ apiCtx context.Context ,
1268
+ forceCallable bool , returnByValue bool ,
1269
+ pageFunc goja.Value , args ... goja.Value ,
1270
+ ) (res interface {}, err error )
1271
+
1272
+ // getInjectedScript returns a JS handle to the injected script of helper
1273
+ // functions.
1274
+ getInjectedScript (apiCtx context.Context ) (api.JSHandle , error )
1275
+
1276
+ // Evaluate will evaluate provided page function within this execution
1277
+ // context.
1278
+ Evaluate (
1279
+ apiCtx context.Context ,
1280
+ pageFunc goja.Value , args ... goja.Value ,
1281
+ ) (interface {}, error )
1282
+
1283
+ // EvaluateHandle will evaluate provided page function within this
1284
+ // execution context.
1285
+ EvaluateHandle (
1286
+ apiCtx context.Context ,
1287
+ pageFunc goja.Value , args ... goja.Value ,
1288
+ ) (api.JSHandle , error )
1289
+
1290
+ // Frame returns the frame that this execution context belongs to.
1291
+ Frame () * Frame
1292
+
1293
+ // id returns the CDP runtime ID of this execution context.
1294
+ ID () runtime.ExecutionContextID
1295
+ }
0 commit comments