1717'use strict' ;
1818
1919const log = require ( '../lib/log.js' ) ;
20+ const Audit = require ( '../audits/audit' ) ;
2021
2122class Driver {
22-
2323 static loadPage ( driver , options ) {
2424 // Since a Page.reload command does not let a service worker take over, we
2525 // navigate away and then come back to reload. We do not `waitForLoad` on
@@ -107,15 +107,19 @@ class Driver {
107107 const driver = options . driver ;
108108 const config = options . config ;
109109 const gatherers = config . gatherers ;
110- const loadData = { } ;
110+ const loadData = { traces : { } } ;
111111 let pass = Promise . resolve ( ) ;
112+ let traceName = Audit . DEFAULT_TRACE ;
113+ if ( config . traceName ) {
114+ traceName = config . traceName ;
115+ }
112116
113117 if ( config . trace ) {
114118 pass = pass . then ( _ => {
115- log . log ( 'status' , ' Gathering: trace' ) ;
119+ log . log ( 'status' , ` Gathering: trace " ${ traceName } "` ) ;
116120 return driver . endTrace ( ) . then ( traceContents => {
117- loadData . traceContents = traceContents ;
118- log . log ( 'statusEnd' , ' Gathering: trace' ) ;
121+ loadData . traces [ traceName ] = { traceContents} ;
122+ log . log ( 'statusEnd' , ` Gathering: trace " ${ traceName } "` ) ;
119123 } ) ;
120124 } ) ;
121125 }
@@ -136,6 +140,9 @@ class Driver {
136140 return chain . then ( _ => {
137141 const status = `Gathering: ${ gatherer . name } ` ;
138142 log . log ( 'status' , status ) ;
143+ if ( config . trace ) {
144+ loadData . traceContents = loadData . traces [ traceName ] . traceContents ;
145+ }
139146 return Promise . resolve ( gatherer . afterPass ( options , loadData ) ) . then ( ret => {
140147 log . log ( 'statusEnd' , status ) ;
141148 return ret ;
@@ -155,7 +162,7 @@ class Driver {
155162
156163 static run ( passes , options ) {
157164 const driver = options . driver ;
158- const tracingData = { } ;
165+ const tracingData = { traces : { } } ;
159166
160167 if ( typeof options . url !== 'string' || options . url . length === 0 ) {
161168 return Promise . reject ( new Error ( 'You must provide a url to the driver' ) ) ;
@@ -190,6 +197,9 @@ class Driver {
190197 . then ( _ => this . pass ( runOptions ) )
191198 . then ( _ => this . afterPass ( runOptions ) )
192199 . then ( loadData => {
200+ // Need to manually merge traces property before
201+ // merging loadDat into tracingData to avoid data loss.
202+ Object . assign ( loadData . traces , tracingData . traces ) ;
193203 Object . assign ( tracingData , loadData ) ;
194204 } )
195205 . then ( _ => this . tearDown ( runOptions ) ) ;
@@ -212,7 +222,6 @@ class Driver {
212222 artifacts [ gatherer . name ] = gatherer . artifact ;
213223 } ) ;
214224 } ) ;
215-
216225 return artifacts ;
217226 } ) ;
218227 }
0 commit comments