Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit fd30521

Browse files
committed
Apply review comments
+ Move frame execution context to frame.go + Remove the unnecessary Errorf in Frame.document + Make the test robust
1 parent d8fbefa commit fd30521

File tree

3 files changed

+58
-76
lines changed

3 files changed

+58
-76
lines changed

common/frame.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (f *Frame) document() (*ElementHandle, error) {
319319
return nil, fmt.Errorf("frame document: cannot evaluate in main execution context: %w", err)
320320
}
321321
if result == nil {
322-
return nil, fmt.Errorf("frame document: evaluate result is nil in main execution context: %w", err)
322+
return nil, errors.New("frame document: evaluate result is nil in main execution context")
323323
}
324324

325325
f.documentHandle = result.(*ElementHandle)
@@ -1249,3 +1249,46 @@ func (f *Frame) WaitForTimeout(timeout int64) {
12491249
case <-time.After(time.Duration(timeout) * time.Millisecond):
12501250
}
12511251
}
1252+
1253+
// frameExecutionContext represents a JS execution context that belongs to Frame.
1254+
type frameExecutionContext interface {
1255+
// adoptBackendNodeId adopts specified backend node into this execution
1256+
// context from another execution context.
1257+
adoptBackendNodeId(backendNodeID cdp.BackendNodeID) (*ElementHandle, error)
1258+
1259+
// adoptElementHandle adopts the specified element handle into this
1260+
// execution context from another execution context.
1261+
adoptElementHandle(elementHandle *ElementHandle) (*ElementHandle, error)
1262+
1263+
// evaluate will evaluate provided callable within this execution
1264+
// context and return by value or handle.
1265+
evaluate(
1266+
apiCtx context.Context,
1267+
forceCallable bool, returnByValue bool,
1268+
pageFunc goja.Value, args ...goja.Value,
1269+
) (res interface{}, err error)
1270+
1271+
// getInjectedScript returns a JS handle to the injected script of helper
1272+
// functions.
1273+
getInjectedScript(apiCtx context.Context) (api.JSHandle, error)
1274+
1275+
// Evaluate will evaluate provided page function within this execution
1276+
// context.
1277+
Evaluate(
1278+
apiCtx context.Context,
1279+
pageFunc goja.Value, args ...goja.Value,
1280+
) (interface{}, error)
1281+
1282+
// EvaluateHandle will evaluate provided page function within this
1283+
// execution context.
1284+
EvaluateHandle(
1285+
apiCtx context.Context,
1286+
pageFunc goja.Value, args ...goja.Value,
1287+
) (api.JSHandle, error)
1288+
1289+
// Frame returns the frame that this execution context belongs to.
1290+
Frame() *Frame
1291+
1292+
// id returns the CDP runtime ID of this execution context.
1293+
ID() runtime.ExecutionContextID
1294+
}

common/frame_execution_context.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

common/frame_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ package common
2323
import (
2424
"context"
2525
"testing"
26+
"time"
2627

2728
"github.com/chromedp/cdproto/cdp"
2829
"github.com/dop251/goja"
2930
"github.com/stretchr/testify/require"
3031
)
3132

32-
func TestFrameDocument_Issue53(t *testing.T) {
33+
// Test calling Frame.document does not panic with a nil document.
34+
// See: issue #53 for details.
35+
func TestFrameNilDocument(t *testing.T) {
3336
t.Parallel()
3437

3538
ctx := context.Background()
@@ -45,7 +48,16 @@ func TestFrameDocument_Issue53(t *testing.T) {
4548
}
4649

4750
// document() waits for the main execution context
48-
go frame.setContext("main", stub)
51+
ok := make(chan struct{}, 1)
52+
go func() {
53+
frame.setContext("main", stub)
54+
ok <- struct{}{}
55+
}()
56+
select {
57+
case <-ok:
58+
case <-time.After(time.Second):
59+
require.FailNow(t, "cannot set the main execution context, frame.setContext timed out")
60+
}
4961

5062
require.NotPanics(t, func() {
5163
_, err := frame.document()

0 commit comments

Comments
 (0)