Skip to content

Commit 17c9dc3

Browse files
kblokMeir017
authored andcommitted
return the trace from tracing.stop (#433)
1 parent 859d4d7 commit 17c9dc3

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

lib/PuppeteerSharp.Tests/TracingTests/TracingTests.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace PuppeteerSharp.Tests.TracingTests
1414
[Collection("PuppeteerLoaderFixture collection")]
1515
public class TracingTests : PuppeteerPageBaseTest
1616
{
17-
private string _file;
18-
17+
private readonly string _file;
18+
1919
public TracingTests(ITestOutputHelper output) : base(output)
2020
{
2121
_file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
@@ -25,7 +25,7 @@ protected override async Task DisposeAsync()
2525
{
2626
await base.DisposeAsync();
2727

28-
int attempts = 0;
28+
var attempts = 0;
2929
const int maxAttempts = 5;
3030

3131
while (true)
@@ -93,19 +93,45 @@ public async Task ShouldThrowIfTracingOnTwoPages()
9393
{
9494
await Page.Tracing.StartAsync(new TracingOptions
9595
{
96-
Path = _file,
96+
Path = _file
9797
});
9898
var newPage = await Browser.NewPageAsync();
9999
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
100100
{
101101
await Page.Tracing.StartAsync(new TracingOptions
102102
{
103-
Path = _file,
103+
Path = _file
104104
});
105105
});
106106

107107
await newPage.CloseAsync();
108108
await Page.Tracing.StopAsync();
109109
}
110+
111+
[Fact]
112+
public async Task ShouldReturnABuffer()
113+
{
114+
await Page.Tracing.StartAsync(new TracingOptions
115+
{
116+
Screenshots = true,
117+
Path = _file
118+
});
119+
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
120+
var trace = await Page.Tracing.StopAsync();
121+
var buf = File.ReadAllText(_file);
122+
Assert.Equal(trace, buf);
123+
}
124+
125+
[Fact]
126+
public async Task ShouldSupportABufferWithoutAPath()
127+
{
128+
await Page.Tracing.StartAsync(new TracingOptions
129+
{
130+
Screenshots = true
131+
});
132+
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
133+
var trace = await Page.Tracing.StopAsync();
134+
Assert.Contains("screenshot", trace);
135+
}
110136
}
111-
}
137+
}

lib/PuppeteerSharp/Tracing.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
45
using System.Threading.Tasks;
56
using 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

Comments
 (0)