Skip to content

Commit 17e9790

Browse files
E2E tests for startup error notification
1 parent b8e0cb2 commit 17e9790

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.E2ETesting;
7+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
8+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
9+
using OpenQA.Selenium;
10+
using Xunit.Abstractions;
11+
using Xunit;
12+
13+
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
14+
{
15+
public class StartupErrorNotificationTest : ServerTestBase<DevHostServerFixture<Program>>
16+
{
17+
public StartupErrorNotificationTest(
18+
BrowserFixture browserFixture,
19+
DevHostServerFixture<Program> serverFixture,
20+
ITestOutputHelper output)
21+
: base(browserFixture, serverFixture, output)
22+
{
23+
}
24+
25+
[Theory]
26+
[InlineData(false)]
27+
[InlineData(true)]
28+
public void DisplaysNotificationForStartupException(bool errorIsAsync)
29+
{
30+
var url = $"{ServerPathBase}?error={(errorIsAsync ? "async" : "sync")}";
31+
32+
Navigate(url, noReload: true);
33+
var errorUiElem = Browser.Exists(By.Id("blazor-error-ui"), TimeSpan.FromSeconds(10));
34+
Assert.NotNull(errorUiElem);
35+
36+
Browser.Equal("block", () => errorUiElem.GetCssValue("display"));
37+
}
38+
}
39+
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// 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

4+
using System;
45
using System.Globalization;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Blazor.Hosting;
8+
using Mono.WebAssembly.Interop;
79

810
namespace BasicTestApp
911
{
1012
public class Program
1113
{
1214
public static async Task Main(string[] args)
1315
{
14-
// Later on, the startup APIs will be inherently asynchronous
15-
// Until then, use this artificial means to show async main is working
16-
await Task.Yield();
16+
await SimulateErrorsIfNeededForTest();
1717

1818
// We want the culture to be en-US so that the tests for bind can work consistently.
1919
CultureInfo.CurrentCulture = new CultureInfo("en-US");
@@ -24,5 +24,22 @@ public static async Task Main(string[] args)
2424
public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
2525
BlazorWebAssemblyHost.CreateDefaultBuilder()
2626
.UseBlazorStartup<Startup>();
27+
28+
// Supports E2E tests in StartupErrorNotificationTest
29+
private static async Task SimulateErrorsIfNeededForTest()
30+
{
31+
var currentUrl = new MonoWebAssemblyJSRuntime().Invoke<string>("getCurrentUrl");
32+
if (currentUrl.Contains("error=sync"))
33+
{
34+
throw new InvalidTimeZoneException("This is a synchronous startup exception");
35+
}
36+
37+
await Task.Yield();
38+
39+
if (currentUrl.Contains("error=async"))
40+
{
41+
throw new InvalidTimeZoneException("This is an asynchronous startup exception");
42+
}
43+
}
2744
}
2845
}

src/Components/test/testassets/BasicTestApp/wwwroot/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
<body>
1515
<root>Loading...</root>
1616

17+
<div id="blazor-error-ui">
18+
An unhandled error has occurred.
19+
<a href class='reload'>Reload</a>
20+
<a class='dismiss' style="cursor: pointer;">🗙</a>
21+
</div>
22+
1723
<!-- Used for testing interop scenarios between JS and .NET -->
1824
<script src="js/jsinteroptests.js"></script>
1925

@@ -27,6 +33,10 @@
2733
function navigationManagerNavigate() {
2834
Blazor.navigateTo('/subdir/some-path');
2935
}
36+
37+
function getCurrentUrl() {
38+
return location.href;
39+
}
3040
</script>
3141
<script src="_framework/blazor.webassembly.js"></script>
3242

src/Components/test/testassets/BasicTestApp/wwwroot/style.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@
77
}
88

99
#blazor-error-ui {
10+
background: lightyellow;
11+
bottom: 0;
12+
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
1013
display: none;
14+
left: 0;
15+
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
16+
position: fixed;
17+
width: 100%;
18+
z-index: 1000;
1119
}
1220

13-
#blazor-error-ui dismiss {
21+
#blazor-error-ui .dismiss {
1422
cursor: pointer;
23+
position: absolute;
24+
right: 0.75rem;
25+
top: 0.5rem;
1526
}
1627

1728
.validation-message {

0 commit comments

Comments
 (0)