Skip to content

Commit 43a6530

Browse files
Add E2E tests
1 parent 3a217e8 commit 43a6530

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed

src/Components/test/E2ETest/Tests/ErrorNotificationTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected override void InitializeAsyncCore()
2929
Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client);
3030
Browser.MountTestComponent<ErrorComponent>();
3131
Browser.Exists(By.Id("blazor-error-ui"));
32-
Browser.Exists(By.TagName("button"));
32+
Browser.Exists(By.Id("throw-simple-exception"));
3333
}
3434

3535
[Fact]
@@ -38,7 +38,7 @@ public void ShowsErrorNotification_OnError_Dismiss()
3838
var errorUi = Browser.FindElement(By.Id("blazor-error-ui"));
3939
Assert.Equal("none", errorUi.GetCssValue("display"));
4040

41-
var causeErrorButton = Browser.FindElement(By.TagName("button"));
41+
var causeErrorButton = Browser.FindElement(By.Id("throw-simple-exception"));
4242
causeErrorButton.Click();
4343

4444
Browser.Exists(By.CssSelector("#blazor-error-ui[style='display: block;']"), TimeSpan.FromSeconds(10));
@@ -52,7 +52,7 @@ public void ShowsErrorNotification_OnError_Dismiss()
5252
[Fact]
5353
public void ShowsErrorNotification_OnError_Reload()
5454
{
55-
var causeErrorButton = Browser.Exists(By.TagName("button"));
55+
var causeErrorButton = Browser.Exists(By.Id("throw-simple-exception"));
5656
var errorUi = Browser.FindElement(By.Id("blazor-error-ui"));
5757
Assert.Equal("none", errorUi.GetCssValue("display"));
5858

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using BasicTestApp;
6+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
7+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
8+
using Microsoft.AspNetCore.E2ETesting;
9+
using OpenQA.Selenium;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
14+
{
15+
public class WebAssemblyLoggingTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
16+
{
17+
public WebAssemblyLoggingTest(
18+
BrowserFixture browserFixture,
19+
ToggleExecutionModeServerFixture<Program> serverFixture,
20+
ITestOutputHelper output)
21+
: base(browserFixture, serverFixture, output)
22+
{
23+
}
24+
25+
protected override void InitializeAsyncCore()
26+
{
27+
Navigate(ServerPathBase, noReload: false);
28+
Browser.MountTestComponent<ErrorComponent>();
29+
Browser.Exists(By.Id("blazor-error-ui"));
30+
31+
var errorUi = Browser.FindElement(By.Id("blazor-error-ui"));
32+
Assert.Equal("none", errorUi.GetCssValue("display"));
33+
}
34+
35+
[Fact]
36+
public void LogsSimpleExceptionsUsingLogger()
37+
{
38+
Browser.FindElement(By.Id("throw-simple-exception")).Click();
39+
Browser.Exists(By.CssSelector("#blazor-error-ui[style='display: block;']"), TimeSpan.FromSeconds(10));
40+
AssertLogContainsCriticalMessages(
41+
"[Custom logger] Unhandled exception",
42+
"[System.InvalidTimeZoneException]: Doing something that won't work!",
43+
"at BasicTestApp.ErrorComponent.ThrowSimple");
44+
}
45+
46+
[Fact]
47+
public void LogsInnerExceptionsUsingLogger()
48+
{
49+
Browser.FindElement(By.Id("throw-inner-exception")).Click();
50+
Browser.Exists(By.CssSelector("#blazor-error-ui[style='display: block;']"), TimeSpan.FromSeconds(10));
51+
AssertLogContainsCriticalMessages(
52+
"[Custom logger] Unhandled exception",
53+
"[System.InvalidTimeZoneException]: Here is the outer exception",
54+
"at BasicTestApp.ErrorComponent.ThrowInner",
55+
"[System.ArithmeticException]: Here is the inner exception");
56+
}
57+
58+
[Fact]
59+
public void LogsAggregateExceptionsUsingLogger()
60+
{
61+
Browser.FindElement(By.Id("throw-aggregate-exception")).Click();
62+
Browser.Exists(By.CssSelector("#blazor-error-ui[style='display: block;']"), TimeSpan.FromSeconds(10));
63+
AssertLogContainsCriticalMessages(
64+
"[Custom logger] Unhandled exception",
65+
"[System.AggregateException]: One or more errors occurred. (Aggregate exception 1) (Aggregate exception 2) (Aggregate exception 3)",
66+
"at BasicTestApp.ErrorComponent.ThrowAggregate",
67+
"[System.InvalidTimeZoneException]: Aggregate exception 1",
68+
"[System.InvalidTimeZoneException]: Aggregate exception 2",
69+
"[System.InvalidTimeZoneException]: Aggregate exception 3");
70+
}
71+
72+
[Fact]
73+
public void LogsUsingCustomLogger()
74+
{
75+
Browser.MountTestComponent<LoggingComponent>();
76+
Browser.Exists(By.Id("blazor-error-ui"));
77+
Browser.Exists(By.Id("log-trace"));
78+
79+
Browser.FindElement(By.Id("log-none")).Click();
80+
AssertLastLogMessage(LogLevel.Info, "[Custom logger] This is a None message with count=1");
81+
82+
Browser.FindElement(By.Id("log-trace")).Click();
83+
AssertLastLogMessage(LogLevel.Debug, "[Custom logger] This is a Trace message with count=2");
84+
85+
Browser.FindElement(By.Id("log-debug")).Click();
86+
AssertLastLogMessage(LogLevel.Debug, "[Custom logger] This is a Debug message with count=3");
87+
88+
Browser.FindElement(By.Id("log-information")).Click();
89+
AssertLastLogMessage(LogLevel.Info, "[Custom logger] This is a Information message with count=4");
90+
91+
Browser.FindElement(By.Id("log-warning")).Click();
92+
AssertLastLogMessage(LogLevel.Warning, "[Custom logger] This is a Warning message with count=5");
93+
94+
Browser.FindElement(By.Id("log-error")).Click();
95+
AssertLastLogMessage(LogLevel.Severe, "[Custom logger] This is a Error message with count=6");
96+
97+
// All the preceding levels don't cause the error UI to appear
98+
var errorUi = Browser.FindElement(By.Id("blazor-error-ui"));
99+
Assert.Equal("none", errorUi.GetCssValue("display"));
100+
101+
// ... but "Critical" level does
102+
Browser.FindElement(By.Id("log-critical")).Click();
103+
AssertLastLogMessage(LogLevel.Severe, "[Custom logger] This is a Critical message with count=7");
104+
Assert.Equal("block", errorUi.GetCssValue("display"));
105+
}
106+
107+
void AssertLastLogMessage(LogLevel level, string message)
108+
{
109+
var log = Browser.Manage().Logs.GetLog(LogType.Browser);
110+
var lastEntry = log[log.Count - 1];
111+
Assert.Equal(level, lastEntry.Level);
112+
113+
// Selenium prefixes the message with various bits of internal info, so use "Contains"
114+
Assert.Contains(message, lastEntry.Message);
115+
}
116+
117+
void AssertLogContainsCriticalMessages(params string[] messages)
118+
{
119+
var log = Browser.Manage().Logs.GetLog(LogType.Browser);
120+
foreach (var message in messages)
121+
{
122+
Assert.Contains(log, entry =>
123+
{
124+
return entry.Level == LogLevel.Severe
125+
&& entry.Message.Contains(message);
126+
});
127+
}
128+
}
129+
}
130+
}

src/Components/test/testassets/BasicTestApp/ErrorComponent.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<h2>Error throwing buttons</h2>
33
<p>
44
<button id="throw-simple-exception" @onclick="@(ThrowSimple)">Throw simple exception</button>
5-
<button id="throw-nested-exception" @onclick="@(ThrowInner)">Throw with inner exception</button>
6-
<button id="throw-nested-exception" @onclick="@(ThrowAggregate)">Throw aggregate exception</button>
5+
<button id="throw-inner-exception" @onclick="@(ThrowInner)">Throw with inner exception</button>
6+
<button id="throw-aggregate-exception" @onclick="@(ThrowAggregate)">Throw aggregate exception</button>
77
</p>
88
</div>
99

src/Components/test/testassets/BasicTestApp/LoggingComponent.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
<h2>Logging buttons</h2>
55
<p>
6-
<button id="log-trace" @onclick="@(() => Log(LogLevel.None))">Log None</button>
6+
<button id="log-none" @onclick="@(() => Log(LogLevel.None))">Log None</button>
77
<button id="log-trace" @onclick="@(() => Log(LogLevel.Trace))">Log Trace</button>
88
<button id="log-debug" @onclick="@(() => Log(LogLevel.Debug))">Log Debug</button>
99
<button id="log-information" @onclick="@(() => Log(LogLevel.Information))">Log Information</button>
1010
<button id="log-warning" @onclick="@(() => Log(LogLevel.Warning))">Log Warning</button>
1111
<button id="log-error" @onclick="@(() => Log(LogLevel.Error))">Log Error</button>
12-
<button id="log-error" @onclick="@(() => Log(LogLevel.Critical))">Log Critical</button>
12+
<button id="log-critical" @onclick="@(() => Log(LogLevel.Critical))">Log Critical</button>
1313
</p>
1414

1515
@code {

0 commit comments

Comments
 (0)