11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . Text ;
45using System . Threading . Tasks ;
56using PuppeteerSharp . Messaging ;
67
@@ -25,7 +26,7 @@ public class Tracing
2526 private readonly CDPSession _client ;
2627 private bool _recording ;
2728 private string _path ;
28- private static readonly List < string > _defaultCategories = new List < string > ( )
29+ private static readonly List < string > _defaultCategories = new List < string >
2930 {
3031 "-*" ,
3132 "devtools.timeline" ,
@@ -57,12 +58,6 @@ public async Task StartAsync(TracingOptions options)
5758 throw new InvalidOperationException ( "Cannot start recording trace while already recording trace." ) ;
5859 }
5960
60- if ( string . IsNullOrEmpty ( options . Path ) )
61- {
62- throw new ArgumentException ( "Must specify a path to write trace file to." ) ;
63- }
64-
65-
6661 var categories = options . Categories ?? _defaultCategories ;
6762
6863 if ( options . Screenshots )
@@ -84,15 +79,15 @@ public async Task StartAsync(TracingOptions options)
8479 /// Stops tracing
8580 /// </summary>
8681 /// <returns>Stop task</returns>
87- public async Task StopAsync ( )
82+ public async Task < string > StopAsync ( )
8883 {
89- var taskWrapper = new TaskCompletionSource < bool > ( ) ;
84+ var taskWrapper = new TaskCompletionSource < string > ( ) ;
9085
9186 async void EventHandler ( object sender , TracingCompleteEventArgs e )
9287 {
93- await ReadStream ( e . Stream , _path ) ;
88+ var tracingData = await ReadStream ( e . Stream , _path ) ;
9489 _client . TracingComplete -= EventHandler ;
95- taskWrapper . SetResult ( true ) ;
90+ taskWrapper . SetResult ( tracingData ) ;
9691 }
9792
9893 _client . TracingComplete += EventHandler ;
@@ -101,31 +96,40 @@ async void EventHandler(object sender, TracingCompleteEventArgs e)
10196
10297 _recording = false ;
10398
104- await taskWrapper . Task ;
99+ return await taskWrapper . Task ;
105100 }
106101
107- private async Task ReadStream ( string stream , string path )
102+ private async Task < string > ReadStream ( string stream , string path )
108103 {
109- using ( var fs = new StreamWriter ( path ) )
110- {
111- bool eof = false ;
104+ var result = new StringBuilder ( ) ;
105+ var eof = false ;
112106
113- while ( ! eof )
107+ while ( ! eof )
108+ {
109+ var response = await _client . SendAsync < IOReadResponse > ( "IO.read" , new
114110 {
115- var response = await _client . SendAsync < IOReadResponse > ( "IO.read" , new
116- {
117- handle = stream
118- } ) ;
111+ handle = stream
112+ } ) ;
119113
120- eof = response . Eof ;
114+ eof = response . Eof ;
115+
116+ result . Append ( response . Data ) ;
117+ }
121118
122- await fs . WriteAsync ( response . Data ) ;
123- }
124- }
119+ if ( ! string . IsNullOrEmpty ( path ) )
120+ {
121+ using ( var fs = new StreamWriter ( path ) )
122+ {
123+ await fs . WriteAsync ( result . ToString ( ) ) ;
124+ }
125+ }
126+
125127 await _client . SendAsync ( "IO.close" , new
126128 {
127129 handle = stream
128130 } ) ;
131+
132+ return result . ToString ( ) ;
129133 }
130134 }
131135}
0 commit comments