Skip to content

Commit d3ad343

Browse files
committed
Cleanups
1 parent 9ad6cf7 commit d3ad343

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

src/ProjectTemplates/BlazorTemplates.Tests/BlazorServerTemplateTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ public async Task BlazorServerTemplateWorks_IndividualAuth(BrowserKind browserKi
191191

192192
private async Task TestBasicNavigation(IPage page)
193193
{
194-
IWebSocket socket = null;
195-
await page.WaitForEventAsync(PageEvent.WebSocket, s => (socket = s.WebSocket) != null);
194+
var socket = BrowserContextInfo.Pages[page].WebSockets.SingleOrDefault() ??
195+
(await page.WaitForEventAsync(PageEvent.WebSocket)).WebSocket;
196+
196197
await socket.WaitForEventAsync(WebSocketEvent.FrameReceived);
197198

198199
await page.WaitForSelectorAsync("ul");
@@ -210,6 +211,7 @@ await Task.WhenAll(
210211

211212
// Clicking the counter button works
212213
await Task.WhenAll(
214+
socket.WaitForEventAsync(WebSocketEvent.FrameReceived),
213215
page.WaitForSelectorAsync("h1+p >> text=Current count: 1"),
214216
page.ClickAsync("p+button >> text=Click me"));
215217

src/Shared/BrowserTesting/src/ContextInformation.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ private void AttachToPage(object sender, PageEventArgs args)
3434
logger.LogInformation($"Network trace will be saved at '{_harPath}'");
3535
}
3636

37-
Pages.Add(args.Page, new PageInformation(args.Page, logger));
37+
var pageInfo = new PageInformation(args.Page, logger);
38+
Pages.Add(args.Page, pageInfo);
39+
args.Page.Close += CleanupPage;
40+
args.Page.Crash += CleanupPage;
41+
}
42+
43+
private void CleanupPage(object sender, EventArgs e)
44+
{
45+
var page = (IPage)sender;
46+
if (Pages.TryGetValue(page, out var info))
47+
{
48+
info.Dispose();
49+
Pages.Remove(page);
50+
}
3851
}
3952

4053
internal BrowserContextOptions ConfigureUniqueHarPath(BrowserContextOptions browserContextOptions)

src/Shared/BrowserTesting/src/PageInformation.cs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -9,7 +9,7 @@
99

1010
namespace Microsoft.AspNetCore.BrowserTesting
1111
{
12-
public class PageInformation
12+
public class PageInformation : IDisposable
1313
{
1414
private readonly Page _page;
1515
private readonly ILogger<PageInformation> _logger;
@@ -20,17 +20,25 @@ public class PageInformation
2020

2121
public List<string> PageErrors { get; } = new();
2222

23+
public List<IWebSocket> WebSockets { get; set; } = new();
24+
2325
public PageInformation(Page page, ILogger<PageInformation> logger)
2426
{
2527
page.Console += RecordConsoleMessage;
2628
page.PageError += RecordPageError;
2729
page.RequestFailed += RecordFailedRequest;
30+
page.WebSocket += CaptureWebSocket;
2831
_page = page;
2932
_logger = logger;
3033

3134
_ = LogPageVideoPath();
3235
}
3336

37+
private void CaptureWebSocket(object sender, WebSocketEventArgs e)
38+
{
39+
WebSockets.Add(e.WebSocket);
40+
}
41+
3442
private async Task LogPageVideoPath()
3543
{
3644
try
@@ -48,27 +56,58 @@ private async Task LogPageVideoPath()
4856
}
4957
}
5058

59+
public void Dispose()
60+
{
61+
_page.Console -= RecordConsoleMessage;
62+
_page.PageError -= RecordPageError;
63+
_page.RequestFailed -= RecordFailedRequest;
64+
}
65+
5166
private void RecordFailedRequest(object sender, RequestFailedEventArgs e)
5267
{
53-
_logger.LogError(e.FailureText);
68+
try
69+
{
70+
_logger.LogError(e.FailureText);
71+
}
72+
catch
73+
{
74+
}
5475
FailedRequests.Add(e.FailureText);
5576
}
5677

5778
private void RecordPageError(object sender, PageErrorEventArgs e)
5879
{
5980
// There needs to be a bit of experimentation with this, but message should be a good start.
60-
_logger.LogError(e.Message);
81+
try
82+
{
83+
_logger.LogError(e.Message);
84+
}
85+
catch
86+
{
87+
}
88+
6189
PageErrors.Add(e.Message);
6290
}
6391

6492
private void RecordConsoleMessage(object sender, ConsoleEventArgs e)
6593
{
6694
var message = e.Message;
95+
var messageText = message.Text.Replace(Environment.NewLine, $"{Environment.NewLine} ");
6796
var location = message.Location;
6897

69-
var logMessage = $"[{_page.Url}]{Environment.NewLine} {message.Text}{Environment.NewLine} ({location.URL}:{location.LineNumber}:{location.ColumnNumber})";
70-
_logger.Log(MapLogLevel(message.Type), logMessage);
71-
BrowserConsoleLogs.Add(new LogEntry(message.Text.Replace(Environment.NewLine, $"{Environment.NewLine} "), message.Type));
98+
var logMessage = $"[{_page.Url}]{Environment.NewLine} {messageText}{Environment.NewLine} ({location.URL}:{location.LineNumber}:{location.ColumnNumber})";
99+
100+
try
101+
{
102+
_logger.Log(MapLogLevel(message.Type), logMessage);
103+
}
104+
catch
105+
{
106+
107+
throw;
108+
}
109+
110+
BrowserConsoleLogs.Add(new LogEntry(messageText, message.Type));
72111

73112
LogLevel MapLogLevel(string messageType) => messageType switch
74113
{

0 commit comments

Comments
 (0)