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

Commit 5158557

Browse files
committed
Rename and adjust loggings
1 parent 90934e0 commit 5158557

File tree

11 files changed

+323
-195
lines changed

11 files changed

+323
-195
lines changed

common/browser.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (b *Browser) connect() error {
123123
}
124124

125125
func (b *Browser) disposeContext(id cdp.BrowserContextID) error {
126-
b.logger.Debugf("Browser:disposeContext", "bid::%v", id)
126+
b.logger.Debugf("Browser:disposeContext", "bctxid:%v", id)
127127
action := target.DisposeBrowserContext(id)
128128
if err := action.Do(cdp.WithExecutor(b.ctx, b.conn)); err != nil {
129129
return fmt.Errorf("unable to dispose browser context %T: %w", action, err)
@@ -203,13 +203,13 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) {
203203
if ok {
204204
browserCtx = bctx
205205
}
206-
b.logger.Debugf("Browser:onAttachedToTarget", "sid:%v tid:%v bcid:%v bctx nil=%t", ev.SessionID, ev.TargetInfo.TargetID, ev.TargetInfo.BrowserContextID, bctx == nil)
206+
b.logger.Debugf("Browser:onAttachedToTarget", "sid:%v tid:%v bctxid:%v bctx nil=%t", ev.SessionID, ev.TargetInfo.TargetID, ev.TargetInfo.BrowserContextID, bctx == nil)
207207
b.contextsMu.RUnlock()
208208

209209
// We're not interested in the top-level browser target, other targets or DevTools targets right now.
210210
isDevTools := strings.HasPrefix(ev.TargetInfo.URL, "devtools://devtools")
211211
if ev.TargetInfo.Type == "browser" || ev.TargetInfo.Type == "other" || isDevTools {
212-
b.logger.Debugf("Browser:onAttachedToTarget:returns", "sid:%v tid:%v (devtools)", ev.SessionID, ev.TargetInfo.TargetID)
212+
b.logger.Debugf("Browser:onAttachedToTarget:return", "sid:%v tid:%v (devtools)", ev.SessionID, ev.TargetInfo.TargetID)
213213
return
214214
}
215215

@@ -219,7 +219,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) {
219219
isRunning := atomic.LoadInt64(&b.state) == BrowserStateOpen && b.IsConnected() //b.conn.isConnected()
220220
if _, ok := err.(*websocket.CloseError); !ok && !isRunning {
221221
// If we're no longer connected to browser, then ignore WebSocket errors
222-
b.logger.Debugf("Browser:onAttachedToTarget:background_page:returns", "sid:%v tid:%v websocket err:%v",
222+
b.logger.Debugf("Browser:onAttachedToTarget:background_page:return", "sid:%v tid:%v websocket err:%v",
223223
ev.SessionID, ev.TargetInfo.TargetID, err)
224224
return
225225
}
@@ -306,7 +306,7 @@ func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) {
306306
func(data interface{}) bool {
307307
mu.RLock()
308308
defer mu.RUnlock()
309-
b.logger.Debugf("Browser:newPageInContext:createWaitForEventHandler", "tid:%v bcid:%v", targetID, id)
309+
b.logger.Debugf("Browser:newPageInContext:createWaitForEventHandler", "tid:%v bctxid:%v", targetID, id)
310310
return data.(*Page).targetID == targetID
311311
},
312312
)
@@ -317,20 +317,20 @@ func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) {
317317
mu.Lock()
318318
defer mu.Unlock()
319319
localTargetID = targetID
320-
b.logger.Debugf("Browser:newPageInContext:CreateTargetBlank", "tid:%v bcid:%v", localTargetID, id)
320+
b.logger.Debugf("Browser:newPageInContext:CreateTargetBlank", "tid:%v bctxid:%v", localTargetID, id)
321321
if targetID, err = action.Do(cdp.WithExecutor(b.ctx, b.conn)); err != nil {
322322
errCh <- fmt.Errorf("unable to execute %T: %w", action, err)
323323
}
324324
}()
325325
select {
326326
case <-b.ctx.Done():
327-
b.logger.Debugf("Browser:newPageInContext:<-b.ctx.Done", "tid:%v bcid:%v", localTargetID, id)
327+
b.logger.Debugf("Browser:newPageInContext:<-b.ctx.Done", "tid:%v bctxid:%v", localTargetID, id)
328328
case <-time.After(b.launchOpts.Timeout):
329-
b.logger.Debugf("Browser:newPageInContext:timeout", "tid:%v bcid:%v timeout:%s", localTargetID, id, b.launchOpts.Timeout)
329+
b.logger.Debugf("Browser:newPageInContext:timeout", "tid:%v bctxid:%v timeout:%s", localTargetID, id, b.launchOpts.Timeout)
330330
case c := <-ch:
331-
b.logger.Debugf("Browser:newPageInContext:<-ch", "tid:%v bcid:%v, c:%v", localTargetID, id, c)
331+
b.logger.Debugf("Browser:newPageInContext:<-ch", "tid:%v bctxid:%v, c:%v", localTargetID, id, c)
332332
case err := <-errCh:
333-
b.logger.Debugf("Browser:newPageInContext:<-errCh", "tid:%v bcid:%v, err:%v", localTargetID, id, err)
333+
b.logger.Debugf("Browser:newPageInContext:<-errCh", "tid:%v bctxid:%v, err:%v", localTargetID, id, err)
334334
return nil, err
335335
}
336336
b.pagesMu.RLock()
@@ -381,7 +381,7 @@ func (b *Browser) IsConnected() bool {
381381
func (b *Browser) NewContext(opts goja.Value) api.BrowserContext {
382382
action := target.CreateBrowserContext().WithDisposeOnDetach(true)
383383
browserContextID, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
384-
b.logger.Debugf("Browser:NewContext", "bcid:%v", browserContextID)
384+
b.logger.Debugf("Browser:NewContext", "bctxid:%v", browserContextID)
385385
if err != nil {
386386
k6Throw(b.ctx, "cannot create browser context (%s): %w", browserContextID, err)
387387
}

common/browser_context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,13 @@ func (b *BrowserContext) NewCDPSession() api.CDPSession {
207207

208208
// NewPage creates a new page inside this browser context.
209209
func (b *BrowserContext) NewPage() api.Page {
210-
b.logger.Debugf("BrowserContext:NewPage", "bcid:%v", b.id)
210+
b.logger.Debugf("BrowserContext:NewPage", "bctxid:%v", b.id)
211+
211212
p, err := b.browser.newPageInContext(b.id)
212213
if err != nil {
213214
k6Throw(b.ctx, "cannot create a new page: %w", err)
214215
}
215-
b.logger.Debugf("BrowserContext:NewPage:returns", "bcid:%v ptid:%s", b.id, p.targetID)
216+
b.logger.Debugf("BrowserContext:NewPage:return", "bctxid:%v ptid:%s", b.id, p.targetID)
216217
return p
217218
}
218219

common/connection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,18 @@ func (c *Connection) closeSession(sid target.SessionID, tid target.ID) {
192192
}
193193

194194
func (c *Connection) createSession(info *target.Info) (*Session, error) {
195-
c.logger.Debugf("Connection:createSession", "tid:%v bid::%v type:%s", info.TargetID, info.BrowserContextID, info.Type)
195+
c.logger.Debugf("Connection:createSession", "tid:%v bctxid:%v type:%s", info.TargetID, info.BrowserContextID, info.Type)
196196

197197
var sessionID target.SessionID
198198
var err error
199199
action := target.AttachToTarget(info.TargetID).WithFlatten(true)
200200
if sessionID, err = action.Do(cdp.WithExecutor(c.ctx, c)); err != nil {
201-
c.logger.Debugf("Connection:createSession", "tid:%v bid::%v type:%s err:%v", info.TargetID, info.BrowserContextID, info.Type, err)
201+
c.logger.Debugf("Connection:createSession", "tid:%v bctxid:%v type:%s err:%v", info.TargetID, info.BrowserContextID, info.Type, err)
202202
return nil, err
203203
}
204204
sess := c.getSession(sessionID)
205205
if sess == nil {
206-
c.logger.Warnf("Connection:createSession", "tid:%v bid::%v type:%s sid:%v, session is nil", info.TargetID, info.BrowserContextID, info.Type, sessionID)
206+
c.logger.Warnf("Connection:createSession", "tid:%v bctxid:%v type:%s sid:%v, session is nil", info.TargetID, info.BrowserContextID, info.Type, sessionID)
207207
}
208208
return sess, nil
209209
}

common/element_handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ func (h *ElementHandle) SetChecked(checked bool, opts goja.Value) {
13861386
func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) {
13871387
// TODO: implement
13881388
rt := k6common.GetRuntime(h.ctx)
1389-
k6common.Throw(rt, errors.New("ElementHandle.setInputFiles() has not been implemented yet!"))
1389+
k6common.Throw(rt, errors.New("ElementHandle.setInputFiles() has not been implemented yet"))
13901390
}
13911391

13921392
func (h *ElementHandle) Tap(opts goja.Value) {

common/execution_context.go

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,50 @@ type ExecutionContext struct {
4646
frame *Frame
4747
id runtime.ExecutionContextID
4848
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
4955
}
5056

5157
// NewExecutionContext creates a new JS execution context
5258
func NewExecutionContext(
5359
ctx context.Context, session *Session, frame *Frame,
5460
id runtime.ExecutionContextID, logger *Logger,
5561
) *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{
7263
ctx: ctx,
7364
session: session,
7465
frame: frame,
7566
id: id,
7667
injectedScript: nil,
7768
logger: logger,
7869
}
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
7985
}
8086

8187
// 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)
8593

8694
var (
8795
remoteObj *runtime.RemoteObject
@@ -93,19 +101,31 @@ func (e *ExecutionContext) adoptBackendNodeId(backendNodeID cdp.BackendNodeID) (
93101
WithExecutionContextID(e.id)
94102

95103
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)
97105
}
98106

99107
return NewJSHandle(e.ctx, e.session, e, e.frame, remoteObj, e.logger).AsElement().(*ElementHandle), nil
100108
}
101109

102110
// 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)
107127

108-
if elementHandle.execCtx == e {
128+
if eh.execCtx == e {
109129
panic("Cannot adopt handle that already belongs to this execution context")
110130
}
111131
if e.frame == nil {
@@ -115,18 +135,25 @@ func (e *ExecutionContext) adoptElementHandle(elementHandle *ElementHandle) (*El
115135
var node *cdp.Node
116136
var err error
117137

118-
action := dom.DescribeNode().WithObjectID(elementHandle.remoteObject.ObjectID)
138+
action := dom.DescribeNode().WithObjectID(eh.remoteObject.ObjectID)
119139
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)
121141
}
122142

123-
return e.adoptBackendNodeId(node.BackendNodeID)
143+
return e.adoptBackendNodeID(node.BackendNodeID)
124144
}
125145

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)
130157

131158
suffix := `//# sourceURL=` + evaluationScriptURL
132159

@@ -181,21 +208,21 @@ func (e *ExecutionContext) evaluate(apiCtx context.Context, forceCallable bool,
181208
}
182209

183210
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"
187214
)
188-
action := runtime.CallFunctionOn(functionOn).
215+
action := runtime.CallFunctionOn(expressionWithSourceURL).
189216
WithArguments(arguments).
190217
WithExecutionContextID(e.id).
191218
WithReturnByValue(returnByValue).
192219
WithAwaitPromise(true).
193220
WithUserGesture(true)
194221
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)
196223
}
197224
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)
199226
}
200227
if remoteObject == nil {
201228
return
@@ -216,8 +243,10 @@ func (e *ExecutionContext) evaluate(apiCtx context.Context, forceCallable bool,
216243

217244
// getInjectedScript returns a JS handle to the injected script of helper functions
218245
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)
221250

222251
if e.injectedScript == nil {
223252
rt := k6common.GetRuntime(e.ctx)
@@ -239,12 +268,18 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
239268
}
240269

241270
// 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) {
243275
return e.evaluate(apiCtx, true, true, pageFunc, args...)
244276
}
245277

246278
// 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) {
248283
res, err := e.evaluate(apiCtx, true, false, pageFunc, args...)
249284
if err != nil {
250285
return nil, err

0 commit comments

Comments
 (0)