|
| 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 | +} |
0 commit comments