|
| 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 WebAssemblyLazyLoadTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>> |
| 16 | + { |
| 17 | + public WebAssemblyLazyLoadTest( |
| 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<TestRouter>(); |
| 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 CanLazyLoadOnRouteChange() |
| 37 | + { |
| 38 | + // Navigate to a page without any lazy-loaded dependencies |
| 39 | + SetUrlViaPushState("/"); |
| 40 | + var app = Browser.MountTestComponent<TestRouter>(); |
| 41 | + |
| 42 | + // Ensure that we haven't requested the lazy loaded assembly |
| 43 | + Assert.False(HasLoadedAssembly("Newtonsoft.Json.dll")); |
| 44 | + |
| 45 | + // Visit the route for the lazy-loaded assembly |
| 46 | + SetUrlViaPushState("/WithDynamicAssembly"); |
| 47 | + |
| 48 | + var button = app.FindElement(By.Id("use-package-button")); |
| 49 | + |
| 50 | + // Now we should have requested the DLL |
| 51 | + Assert.True(HasLoadedAssembly("Newtonsoft.Json.dll")); |
| 52 | + |
| 53 | + button.Click(); |
| 54 | + |
| 55 | + // We shouldn't get any errors about assemblies not being available |
| 56 | + AssertLogDoesNotContainCriticalMessages("Could not load file or assembly 'Newtonsoft.Json"); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void CanLazyLoadOnFirstVisit() |
| 61 | + { |
| 62 | + // Navigate to a page with lazy loaded assemblies for the first time |
| 63 | + SetUrlViaPushState("/WithDynamicAssembly"); |
| 64 | + var app = Browser.MountTestComponent<TestRouter>(); |
| 65 | + var button = app.FindElement(By.Id("use-package-button")); |
| 66 | + |
| 67 | + // We should have requested the DLL |
| 68 | + Assert.True(HasLoadedAssembly("Newtonsoft.Json.dll")); |
| 69 | + |
| 70 | + button.Click(); |
| 71 | + |
| 72 | + // We shouldn't get any errors about assemblies not being available |
| 73 | + AssertLogDoesNotContainCriticalMessages("Could not load file or assembly 'Newtonsoft.Json"); |
| 74 | + } |
| 75 | + |
| 76 | + private string SetUrlViaPushState(string relativeUri) |
| 77 | + { |
| 78 | + var pathBaseWithoutHash = ServerPathBase.Split('#')[0]; |
| 79 | + var jsExecutor = (IJavaScriptExecutor)Browser; |
| 80 | + var absoluteUri = new Uri(_serverFixture.RootUri, $"{pathBaseWithoutHash}{relativeUri}"); |
| 81 | + jsExecutor.ExecuteScript($"Blazor.navigateTo('{absoluteUri.ToString().Replace("'", "\\'")}')"); |
| 82 | + |
| 83 | + return absoluteUri.AbsoluteUri; |
| 84 | + } |
| 85 | + |
| 86 | + private bool HasLoadedAssembly(string name) |
| 87 | + { |
| 88 | + var checkScript = $"return window.performance.getEntriesByType('resource').some(r => r.name.endsWith('{name}'));"; |
| 89 | + var jsExecutor = (IJavaScriptExecutor)Browser; |
| 90 | + var nameRequested = jsExecutor.ExecuteScript(checkScript); |
| 91 | + if (nameRequested != null) |
| 92 | + { |
| 93 | + return (bool)nameRequested; |
| 94 | + } |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + private void AssertLogDoesNotContainCriticalMessages(params string[] messages) |
| 99 | + { |
| 100 | + var log = Browser.Manage().Logs.GetLog(LogType.Browser); |
| 101 | + foreach (var message in messages) |
| 102 | + { |
| 103 | + Assert.DoesNotContain(log, entry => |
| 104 | + { |
| 105 | + return entry.Level == LogLevel.Severe |
| 106 | + && entry.Message.Contains(message); |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments