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

Commit 5398ca3

Browse files
committed
Add logs to BrowserContext
1 parent 5158557 commit 5398ca3

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

common/browser_context.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (b *BrowserContext) AddCookies(cookies goja.Value) {
7777

7878
// AddInitScript adds a script that will be initialized on all new pages.
7979
func (b *BrowserContext) AddInitScript(script goja.Value, arg goja.Value) {
80-
b.logger.Debugf("BrowserContext:AddInitScript", "")
80+
b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id)
8181

8282
rt := k6common.GetRuntime(b.ctx)
8383

@@ -118,6 +118,8 @@ func (b *BrowserContext) Browser() api.Browser {
118118

119119
// ClearCookies clears cookies.
120120
func (b *BrowserContext) ClearCookies() {
121+
b.logger.Debugf("BrowserContext:ClearCookies", "bctxid:%v", b.id)
122+
121123
action := storage.ClearCookies().WithBrowserContextID(b.id)
122124
if err := action.Do(b.ctx); err != nil {
123125
k6Throw(b.ctx, "unable to clear cookies permissions: %w", err)
@@ -126,6 +128,8 @@ func (b *BrowserContext) ClearCookies() {
126128

127129
// ClearPermissions clears any permission overrides.
128130
func (b *BrowserContext) ClearPermissions() {
131+
b.logger.Debugf("BrowserContext:ClearPermissions", "bctxid:%v", b.id)
132+
129133
action := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)
130134
if err := action.Do(b.ctx); err != nil {
131135
k6Throw(b.ctx, "unable to clear override permissions: %w", err)
@@ -134,6 +138,8 @@ func (b *BrowserContext) ClearPermissions() {
134138

135139
// Close shuts down the browser context.
136140
func (b *BrowserContext) Close() {
141+
b.logger.Debugf("BrowserContext:Close", "bctxid:%v", b.id)
142+
137143
if b.id == "" {
138144
k6Throw(b.ctx, "default browser context can't be closed")
139145
}
@@ -157,7 +163,8 @@ func (b *BrowserContext) ExposeFunction(name string, callback goja.Callable) {
157163

158164
// GrantPermissions enables the specified permissions, all others will be disabled.
159165
func (b *BrowserContext) GrantPermissions(permissions []string, opts goja.Value) {
160-
b.logger.Debugf("BrowserContext:GrantPermissions", "")
166+
b.logger.Debugf("BrowserContext:GrantPermissions", "bctxid:%v", b.id)
167+
161168
permsToProtocol := map[string]cdpbrowser.PermissionType{
162169
"geolocation": cdpbrowser.PermissionTypeGeolocation,
163170
"midi": cdpbrowser.PermissionTypeMidi,
@@ -232,11 +239,15 @@ func (b *BrowserContext) Route(url goja.Value, handler goja.Callable) {
232239

233240
// SetDefaultNavigationTimeout sets the default navigation timeout in milliseconds.
234241
func (b *BrowserContext) SetDefaultNavigationTimeout(timeout int64) {
242+
b.logger.Debugf("BrowserContext:SetDefaultNavigationTimeout", "bctxid:%v timeout:%d", b.id, timeout)
243+
235244
b.timeoutSettings.setDefaultNavigationTimeout(timeout)
236245
}
237246

238247
// SetDefaultTimeout sets the default maximum timeout in milliseconds.
239248
func (b *BrowserContext) SetDefaultTimeout(timeout int64) {
249+
b.logger.Debugf("BrowserContext:SetDefaultTimeout", "bctxid:%v timeout:%d", b.id, timeout)
250+
240251
b.timeoutSettings.setDefaultTimeout(timeout)
241252
}
242253

@@ -246,6 +257,8 @@ func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) {
246257

247258
// SetGeolocation overrides the geo location of the user.
248259
func (b *BrowserContext) SetGeolocation(geolocation goja.Value) {
260+
b.logger.Debugf("BrowserContext:SetGeolocation", "bctxid:%v", b.id)
261+
249262
g := NewGeolocation()
250263
if err := g.Parse(b.ctx, geolocation); err != nil {
251264
k6Throw(b.ctx, "cannot parse geo location: %w", err)
@@ -261,6 +274,8 @@ func (b *BrowserContext) SetGeolocation(geolocation goja.Value) {
261274

262275
// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.
263276
func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) {
277+
b.logger.Debugf("BrowserContext:SetHTTPCredentials", "bctxid:%v", b.id)
278+
264279
c := NewCredentials()
265280
if err := c.Parse(b.ctx, httpCredentials); err != nil {
266281
k6Throw(b.ctx, "cannot set HTTP credentials: %w", err)
@@ -274,6 +289,8 @@ func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) {
274289

275290
// SetOffline toggles the browser's connectivity on/off.
276291
func (b *BrowserContext) SetOffline(offline bool) {
292+
b.logger.Debugf("BrowserContext:SetOffline", "bctxid:%v offline:%t", b.id, offline)
293+
277294
b.opts.Offline = offline
278295
for _, p := range b.browser.getPages() {
279296
p.updateOffline()
@@ -289,8 +306,8 @@ func (b *BrowserContext) Unroute(url goja.Value, handler goja.Callable) {
289306
}
290307

291308
func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) interface{} {
292-
b.logger.Debugf("BrowserContext:WaitForEvent", "event:%q", event)
293309
// TODO: This public API needs Promise support (as return value) to be useful in JS!
310+
b.logger.Debugf("BrowserContext:WaitForEvent", "bctxid:%v event:%q", b.id, event)
294311

295312
rt := k6common.GetRuntime(b.ctx)
296313

@@ -326,16 +343,16 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
326343
ch := make(chan interface{})
327344

328345
go func() {
329-
b.logger.Debugf("BrowserContext:WaitForEvent:go()", "starts")
330-
defer b.logger.Debugf("BrowserContext:WaitForEvent:go()", "returns")
346+
b.logger.Debugf("BrowserContext:WaitForEvent:go():starts", "bctxid:%v", b.id)
347+
defer b.logger.Debugf("BrowserContext:WaitForEvent:go():returns", "bctxid:%v", b.id)
331348
for {
332349
select {
333350
case <-evCancelCtx.Done():
334-
b.logger.Debugf("BrowserContext:WaitForEvent:go()", "evCancelCtx done")
351+
b.logger.Debugf("BrowserContext:WaitForEvent:go():evCancelCtx:done", "bctxid:%v", b.id)
335352
return
336353
case ev := <-chEvHandler:
337354
if ev.typ == EventBrowserContextClose {
338-
b.logger.Debugf("BrowserContext:WaitForEvent:go()", "EventBrowserContextClose returns")
355+
b.logger.Debugf("BrowserContext:WaitForEvent:go():EventBrowserContextClose:return", "bctxid:%v", b.id)
339356
ch <- nil
340357
close(ch)
341358

@@ -345,11 +362,11 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
345362
return
346363
}
347364
if ev.typ == EventBrowserContextPage {
348-
b.logger.Debugf("BrowserContext:WaitForEvent:go()", "EventBrowserContextPage")
365+
b.logger.Debugf("BrowserContext:WaitForEvent:go():EventBrowserContextPage", "bctxid:%v", b.id)
349366
p := ev.data.(*Page)
350367
exported := k6common.Bind(rt, p, &b.ctx)
351368
if retVal, err := predicateFn(rt.ToValue(exported)); err == nil && retVal.ToBoolean() {
352-
b.logger.Debugf("BrowserContext:WaitForEvent:go()", "EventBrowserContextPage returns")
369+
b.logger.Debugf("BrowserContext:WaitForEvent:go():EventBrowserContextPage:return", "bctxid:%v", b.id)
353370
ch <- p
354371
close(ch)
355372

@@ -368,13 +385,13 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
368385

369386
select {
370387
case <-b.ctx.Done():
371-
b.logger.Debugf("BrowserContext:WaitForEvent", "b.ctx Done")
388+
b.logger.Debugf("BrowserContext:WaitForEvent:ctx.Done", "bctxid:%v event:%q", b.id, event)
372389
case <-time.After(timeout):
373-
b.logger.Debugf("BrowserContext:WaitForEvent", "timeout")
390+
b.logger.Debugf("BrowserContext:WaitForEvent:timeout", "bctxid:%v event:%q", b.id, event)
374391
case evData := <-ch:
375-
b.logger.Debugf("BrowserContext:WaitForEvent", "evData")
392+
b.logger.Debugf("BrowserContext:WaitForEvent:evData", "bctxid:%v event:%q", b.id, event)
376393
return evData
377394
}
378-
b.logger.Debugf("BrowserContext:WaitForEvent", "nil return")
395+
b.logger.Debugf("BrowserContext:WaitForEvent:return nil", "bctxid:%v event:%q", b.id, event)
379396
return nil
380397
}

common/frame.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,8 +1375,8 @@ func (f *Frame) WaitForSelector(selector string, opts goja.Value) api.ElementHan
13751375
func (f *Frame) WaitForTimeout(timeout int64) {
13761376
to := time.Duration(timeout) * time.Millisecond
13771377

1378-
f.log.Debugf("Frame:WaitForTimeout", "fid:%s furl:%q to:%s", f.id, f.url, to)
1379-
defer f.log.Debugf("Frame:WaitForTimeout:return", "fid:%s furl:%q to:%s", f.id, f.url, to)
1378+
f.log.Debugf("Frame:WaitForTimeout", "fid:%s furl:%q timeout:%s", f.id, f.url, to)
1379+
defer f.log.Debugf("Frame:WaitForTimeout:return", "fid:%s furl:%q timeout:%s", f.id, f.url, to)
13801380

13811381
select {
13821382
case <-f.ctx.Done():

0 commit comments

Comments
 (0)