@@ -46,42 +46,50 @@ type ExecutionContext struct {
46
46
frame * Frame
47
47
id runtime.ExecutionContextID
48
48
injectedScript api.JSHandle
49
+
50
+ // Used for logging
51
+ sid target.SessionID // Session ID
52
+ stid cdp.FrameID // Session TargetID
53
+ fid cdp.FrameID // Frame ID
54
+ furl string // Frame URL
49
55
}
50
56
51
57
// NewExecutionContext creates a new JS execution context
52
58
func NewExecutionContext (
53
59
ctx context.Context , session * Session , frame * Frame ,
54
60
id runtime.ExecutionContextID , logger * Logger ,
55
61
) * ExecutionContext {
56
- var (
57
- sid target.SessionID
58
- fid cdp.FrameID
59
- furl string
60
- )
61
- if session != nil {
62
- sid = session .id
63
- }
64
- if frame != nil {
65
- fid = frame .id
66
- furl = frame .url
67
- }
68
- logger .Debugf ("NewExecutionContext" , "sid:%s tid:%s ecid:%d furl:%q" ,
69
- sid , fid , id , furl )
70
-
71
- return & ExecutionContext {
62
+ e := & ExecutionContext {
72
63
ctx : ctx ,
73
64
session : session ,
74
65
frame : frame ,
75
66
id : id ,
76
67
injectedScript : nil ,
77
68
logger : logger ,
78
69
}
70
+
71
+ if session != nil {
72
+ e .sid = session .id
73
+ e .stid = cdp .FrameID (session .targetID )
74
+ }
75
+ if frame != nil {
76
+ e .fid = frame .id
77
+ e .furl = frame .url
78
+ }
79
+ logger .Debugf (
80
+ "NewExecutionContext" ,
81
+ "sid:%s stid:%s fid:%s ectxid:%d furl:%q" ,
82
+ e .sid , e .stid , e .fid , id , e .furl )
83
+
84
+ return e
79
85
}
80
86
81
87
// Adopts specified backend node into this execution context from another execution context
82
- func (e * ExecutionContext ) adoptBackendNodeId (backendNodeID cdp.BackendNodeID ) (* ElementHandle , error ) {
83
- e .logger .Debugf ("ExecutionContext:adoptBackendNodeId" , "sid:%s tid:%s tid:%s ecid:%d furl:%q bnid:%d" ,
84
- e .session .id , e .session .targetID , e .frame .id , e .id , e .frame .url , backendNodeID )
88
+ func (e * ExecutionContext ) adoptBackendNodeID (backendNodeID cdp.BackendNodeID ) (* ElementHandle , error ) {
89
+ e .logger .Debugf (
90
+ "ExecutionContext:adoptBackendNodeID" ,
91
+ "sid:%s stid:%s fid:%s ectxid:%d furl:%q bnid:%d" ,
92
+ e .sid , e .stid , e .fid , e .id , e .furl , backendNodeID )
85
93
86
94
var (
87
95
remoteObj * runtime.RemoteObject
@@ -93,19 +101,31 @@ func (e *ExecutionContext) adoptBackendNodeId(backendNodeID cdp.BackendNodeID) (
93
101
WithExecutionContextID (e .id )
94
102
95
103
if remoteObj , err = action .Do (cdp .WithExecutor (e .ctx , e .session )); err != nil {
96
- return nil , fmt .Errorf ("cannot resolve dom node: %w" , err )
104
+ return nil , fmt .Errorf ("cannot resolve DOM node: %w" , err )
97
105
}
98
106
99
107
return NewJSHandle (e .ctx , e .session , e , e .frame , remoteObj , e .logger ).AsElement ().(* ElementHandle ), nil
100
108
}
101
109
102
110
// Adopts the specified element handle into this execution context from another execution context
103
- func (e * ExecutionContext ) adoptElementHandle (elementHandle * ElementHandle ) (* ElementHandle , error ) {
104
- e .logger .Debugf ("ExecutionContext:adoptElementHandle" , "sid:%s tid:%s tid:%s ecid:%d furl:%q ehtid:%s ehsid:%s" ,
105
- e .session .id , e .session .targetID , e .frame .id , e .id , e .frame .url ,
106
- elementHandle .frame .id , elementHandle .session .id )
111
+ func (e * ExecutionContext ) adoptElementHandle (eh * ElementHandle ) (* ElementHandle , error ) {
112
+ var (
113
+ efid cdp.FrameID
114
+ esid target.SessionID
115
+ )
116
+ if eh .frame != nil {
117
+ efid = eh .frame .id
118
+ }
119
+ if eh .session != nil {
120
+ esid = eh .session .id
121
+ }
122
+ e .logger .Debugf (
123
+ "ExecutionContext:adoptElementHandle" ,
124
+ "sid:%s stid:%s fid:%s ectxid:%d furl:%q ehtid:%s ehsid:%s" ,
125
+ e .sid , e .stid , e .fid , e .id , e .furl ,
126
+ efid , esid )
107
127
108
- if elementHandle .execCtx == e {
128
+ if eh .execCtx == e {
109
129
panic ("Cannot adopt handle that already belongs to this execution context" )
110
130
}
111
131
if e .frame == nil {
@@ -115,18 +135,25 @@ func (e *ExecutionContext) adoptElementHandle(elementHandle *ElementHandle) (*El
115
135
var node * cdp.Node
116
136
var err error
117
137
118
- action := dom .DescribeNode ().WithObjectID (elementHandle .remoteObject .ObjectID )
138
+ action := dom .DescribeNode ().WithObjectID (eh .remoteObject .ObjectID )
119
139
if node , err = action .Do (cdp .WithExecutor (e .ctx , e .session )); err != nil {
120
- return nil , fmt .Errorf ("cannot describe dom node: %w" , err )
140
+ return nil , fmt .Errorf ("cannot describe DOM node: %w" , err )
121
141
}
122
142
123
- return e .adoptBackendNodeId (node .BackendNodeID )
143
+ return e .adoptBackendNodeID (node .BackendNodeID )
124
144
}
125
145
126
- // evaluate will evaluate provided callable within this execution context and return by value or handle
127
- func (e * ExecutionContext ) evaluate (apiCtx context.Context , forceCallable bool , returnByValue bool , pageFunc goja.Value , args ... goja.Value ) (res interface {}, err error ) {
128
- e .logger .Debugf ("ExecutionContext:evaluate" , "sid:%s tid:%s tid:%s ecid:%d furl:%q" ,
129
- e .session .id , e .session .targetID , e .frame .id , e .id , e .frame .url )
146
+ // evaluate will evaluate provided callable within this execution context
147
+ // and return by value or handle
148
+ func (e * ExecutionContext ) evaluate (
149
+ apiCtx context.Context ,
150
+ forceCallable bool , returnByValue bool ,
151
+ pageFunc goja.Value , args ... goja.Value ,
152
+ ) (res interface {}, err error ) {
153
+ e .logger .Debugf (
154
+ "ExecutionContext:evaluate" ,
155
+ "sid:%s stid:%s fid:%s ectxid:%d furl:%q forceCallable:%t returnByvalue:%t" ,
156
+ e .sid , e .stid , e .fid , e .id , e .furl , forceCallable , returnByValue )
130
157
131
158
suffix := `//# sourceURL=` + evaluationScriptURL
132
159
@@ -181,21 +208,21 @@ func (e *ExecutionContext) evaluate(apiCtx context.Context, forceCallable bool,
181
208
}
182
209
183
210
var (
184
- remoteObject * runtime.RemoteObject
185
- exceptionDetails * runtime.ExceptionDetails
186
- functionOn = expression + "\n " + suffix + "\n "
211
+ remoteObject * runtime.RemoteObject
212
+ exceptionDetails * runtime.ExceptionDetails
213
+ expressionWithSourceURL = expression + "\n " + suffix + "\n "
187
214
)
188
- action := runtime .CallFunctionOn (functionOn ).
215
+ action := runtime .CallFunctionOn (expressionWithSourceURL ).
189
216
WithArguments (arguments ).
190
217
WithExecutionContextID (e .id ).
191
218
WithReturnByValue (returnByValue ).
192
219
WithAwaitPromise (true ).
193
220
WithUserGesture (true )
194
221
if remoteObject , exceptionDetails , err = action .Do (cdp .WithExecutor (apiCtx , e .session )); err != nil {
195
- return nil , fmt .Errorf ("cannot call function on expression (%q) in execution context (%d): %w" , functionOn , e .id , err )
222
+ return nil , fmt .Errorf ("cannot call function on expression (%q) in execution context (%d): %w" , expressionWithSourceURL , e .id , err )
196
223
}
197
224
if exceptionDetails != nil {
198
- return nil , fmt .Errorf ("cannot call function on expression (%q) in execution context (%d): %w" , functionOn , e .id , err )
225
+ return nil , fmt .Errorf ("cannot call function on expression (%q) in execution context (%d): %w" , expressionWithSourceURL , e .id , err )
199
226
}
200
227
if remoteObject == nil {
201
228
return
@@ -216,8 +243,10 @@ func (e *ExecutionContext) evaluate(apiCtx context.Context, forceCallable bool,
216
243
217
244
// getInjectedScript returns a JS handle to the injected script of helper functions
218
245
func (e * ExecutionContext ) getInjectedScript (apiCtx context.Context ) (api.JSHandle , error ) {
219
- e .logger .Debugf ("ExecutionContext:getInjectedScript" , "sid:%s tid:%s tid:%s ecid:%d furl:%q" ,
220
- e .session .id , e .session .targetID , e .frame .id , e .id , e .frame .url )
246
+ e .logger .Debugf (
247
+ "ExecutionContext:getInjectedScript" ,
248
+ "sid:%s stid:%s fid:%s ectxid:%d efurl:%s" ,
249
+ e .sid , e .stid , e .fid , e .id , e .furl )
221
250
222
251
if e .injectedScript == nil {
223
252
rt := k6common .GetRuntime (e .ctx )
@@ -239,12 +268,18 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
239
268
}
240
269
241
270
// Evaluate will evaluate provided page function within this execution context
242
- func (e * ExecutionContext ) Evaluate (apiCtx context.Context , pageFunc goja.Value , args ... goja.Value ) (interface {}, error ) {
271
+ func (e * ExecutionContext ) Evaluate (
272
+ apiCtx context.Context ,
273
+ pageFunc goja.Value , args ... goja.Value ,
274
+ ) (interface {}, error ) {
243
275
return e .evaluate (apiCtx , true , true , pageFunc , args ... )
244
276
}
245
277
246
278
// EvaluateHandle will evaluate provided page function within this execution context
247
- func (e * ExecutionContext ) EvaluateHandle (apiCtx context.Context , pageFunc goja.Value , args ... goja.Value ) (api.JSHandle , error ) {
279
+ func (e * ExecutionContext ) EvaluateHandle (
280
+ apiCtx context.Context ,
281
+ pageFunc goja.Value , args ... goja.Value ,
282
+ ) (api.JSHandle , error ) {
248
283
res , err := e .evaluate (apiCtx , true , false , pageFunc , args ... )
249
284
if err != nil {
250
285
return nil , err
0 commit comments