@@ -41,6 +41,7 @@ import (
41
41
"github.com/chromedp/cdproto/security"
42
42
"github.com/chromedp/cdproto/target"
43
43
"github.com/grafana/xk6-browser/api"
44
+ "github.com/sirupsen/logrus"
44
45
k6lib "go.k6.io/k6/lib"
45
46
k6stats "go.k6.io/k6/stats"
46
47
)
@@ -77,10 +78,21 @@ type FrameSession struct {
77
78
childSessions map [cdp.FrameID ]* FrameSession
78
79
79
80
logger * Logger
81
+ // logger that will properly serialize RemoteObject instances
82
+ loggerObj * logrus.Logger
80
83
}
81
84
82
- func NewFrameSession (ctx context.Context , session * Session , page * Page , parent * FrameSession , targetID target.ID , logger * Logger ) (* FrameSession , error ) {
85
+ func NewFrameSession (
86
+ ctx context.Context , session * Session , page * Page , parent * FrameSession ,
87
+ targetID target.ID , logger * Logger ,
88
+ ) (* FrameSession , error ) {
83
89
logger .Debugf ("NewFrameSession" , "sid:%v tid:%v" , session .id , targetID )
90
+ state := k6lib .GetState (ctx )
91
+ loggerObj := & logrus.Logger {
92
+ Out : state .Logger .Out ,
93
+ Level : state .Logger .Level ,
94
+ Formatter : & mapAsObjectFormatter {state .Logger .Formatter },
95
+ }
84
96
fs := FrameSession {
85
97
ctx : ctx , // TODO: create cancelable context that can be used to cancel and close all child sessions
86
98
session : session ,
@@ -96,6 +108,7 @@ func NewFrameSession(ctx context.Context, session *Session, page *Page, parent *
96
108
eventCh : make (chan Event ),
97
109
childSessions : make (map [cdp.FrameID ]* FrameSession ),
98
110
logger : logger ,
111
+ loggerObj : loggerObj ,
99
112
}
100
113
var err error
101
114
if fs .parent != nil {
@@ -454,34 +467,39 @@ func (fs *FrameSession) navigateFrame(frame *Frame, url, referrer string) (strin
454
467
}
455
468
456
469
func (fs * FrameSession ) onConsoleAPICalled (event * runtime.EventConsoleAPICalled ) {
457
- // TODO: switch to using browser logger instead of directly outputting to k6 logging system
458
470
state := k6lib .GetState (fs .ctx )
459
- l := state .Logger .
471
+ // TODO: switch to using browser logger instead of directly outputting to k6 logging system
472
+ l := fs .loggerObj .
460
473
WithTime (event .Timestamp .Time ()).
461
474
WithField ("source" , "browser-console-api" )
475
+
462
476
if state .Group .Path != "" {
463
477
l = l .WithField ("group" , state .Group .Path )
464
478
}
465
- var convertedArgs []interface {}
466
- for _ , arg := range event .Args {
467
- i , err := interfaceFromRemoteObject (arg )
479
+
480
+ var parsedObjects []interface {}
481
+ for _ , robj := range event .Args {
482
+ i , err := parseRemoteObject (robj , l )
468
483
if err != nil {
469
- // TODO(fix): this should not throw!
484
+ // If this throws it's a bug :)
470
485
k6Throw (fs .ctx , "unable to parse remote object value: %w" , err )
471
486
}
472
- convertedArgs = append (convertedArgs , i )
487
+ parsedObjects = append (parsedObjects , i )
473
488
}
489
+
490
+ l = l .WithField ("objects" , parsedObjects )
491
+
474
492
switch event .Type {
475
493
case "log" :
476
- l .Info (convertedArgs ... )
494
+ l .Info ()
477
495
case "info" :
478
- l .Info (convertedArgs ... )
496
+ l .Info ()
479
497
case "warning" :
480
- l .Warn (convertedArgs ... )
498
+ l .Warn ()
481
499
case "error" :
482
- l .Error (convertedArgs ... )
500
+ l .Error ()
483
501
default :
484
- l .Debug (convertedArgs ... )
502
+ l .Debug ()
485
503
}
486
504
}
487
505
@@ -517,7 +535,7 @@ func (fs *FrameSession) onExecutionContextCreated(event *runtime.EventExecutionC
517
535
if i .Type == "isolated" {
518
536
fs .isolatedWorlds [event .Context .Name ] = true
519
537
}
520
- context := NewExecutionContext (fs .ctx , fs .session , frame , event .Context .ID )
538
+ context := NewExecutionContext (fs .ctx , fs .session , frame , event .Context .ID , fs . logger )
521
539
if world != "" {
522
540
frame .setContext (world , context )
523
541
}
0 commit comments