From 66d2b0b0f87c97efaece84349fc7d6f767b01d06 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz Date: Tue, 5 Aug 2025 15:48:16 +0200 Subject: [PATCH 01/10] Supress enhanced nav per test. --- .../EnhancedNavigationTest.cs | 36 +++++++++++++------ .../EnhancedNavigationTestUtil.cs | 13 +++++-- .../RazorComponents/App.razor | 4 +-- .../RazorComponents/Root.razor | 4 +-- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs b/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs index 7dfaf3523287..f68805604514 100644 --- a/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs +++ b/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs @@ -677,11 +677,10 @@ public void CanUpdateHrefOnLinkTagWithIntegrity() } [Theory] - [QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/60875")] - // [InlineData(false, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 + [InlineData(false, false, false)] [InlineData(false, true, false)] [InlineData(true, true, false)] - // [InlineData(true, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 + [InlineData(true, false, false)] // [InlineData(false, false, true)] programmatic navigation doesn't work without enhanced navigation [InlineData(false, true, true)] [InlineData(true, true, true)] @@ -692,8 +691,8 @@ public void EnhancedNavigationScrollBehavesSameAsBrowserOnNavigation(bool enable // or to the beginning of a fragment, regardless of the previous scroll position string landingPageSuffix = enableStreaming ? "" : "-no-streaming"; string buttonKeyword = programmaticNavigation ? "-programmatic" : ""; - Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}"); EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation, skipNavigation: true); + Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}"); // "landing" page: scroll maximally down and go to "next" page - we should land at the top of that page AssertWeAreOnLandingPage(); @@ -732,10 +731,10 @@ public void EnhancedNavigationScrollBehavesSameAsBrowserOnNavigation(bool enable } [Theory] - // [InlineData(false, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 + [InlineData(false, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 [InlineData(false, true, false)] [InlineData(true, true, false)] - // [InlineData(true, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 + [InlineData(true, false, false)] // https://github.com/dotnet/aspnetcore/issues/60875 // [InlineData(false, false, true)] programmatic navigation doesn't work without enhanced navigation [InlineData(false, true, true)] [InlineData(true, true, true)] @@ -745,8 +744,8 @@ public void EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsActio // This test checks if the scroll position is preserved after backwards/forwards action string landingPageSuffix = enableStreaming ? "" : "-no-streaming"; string buttonKeyword = programmaticNavigation ? "-programmatic" : ""; - Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}"); EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation, skipNavigation: true); + Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}"); // "landing" page: scroll to pos1, navigate away AssertWeAreOnLandingPage(); @@ -831,6 +830,7 @@ private void AssertScrollPositionCorrect(bool useEnhancedNavigation, long previo private void AssertEnhancedNavigation(bool useEnhancedNavigation, IWebElement elementForStalenessCheck, int retryCount = 3, int delayBetweenRetriesMs = 1000) { bool enhancedNavigationDetected = false; + string logging = ""; for (int i = 0; i < retryCount; i++) { try @@ -841,15 +841,31 @@ private void AssertEnhancedNavigation(bool useEnhancedNavigation, IWebElement el } catch (XunitException) { + var logs = Browser.GetBrowserLogs(LogLevel.Warning); + logging += $"{string.Join(", ", logs.Select(l => l.Message))}\n"; + + var testId = ((IJavaScriptExecutor)Browser).ExecuteScript("return sessionStorage.getItem('test-id');"); + logging += $" testId: {testId}\n"; + if (testId is null) + { + continue; + } + var suppressKey = $"suppress-enhanced-navigation-{testId}"; + + var enhancedNavAttached = ((IJavaScriptExecutor)Browser).ExecuteScript("return sessionStorage.getItem('blazor-enhanced-nav-attached');"); + var suppressEnhancedNavigation = ((IJavaScriptExecutor)Browser).ExecuteScript($"return sessionStorage.getItem('{suppressKey}');"); + + logging += $" suppressKey: {suppressKey}\n"; + logging += $" {suppressKey}: {suppressEnhancedNavigation}\n"; // Maybe the check was done too early to change the DOM ref, retry } Thread.Sleep(delayBetweenRetriesMs); } - string expectedNavigation = useEnhancedNavigation ? "enhanced navigation" : "browser navigation"; + string expectedNavigation = useEnhancedNavigation ? "enhanced navigation" : "full page load"; string isStale = enhancedNavigationDetected ? "is not stale" : "is stale"; - var isNavigationSupressed = (string)((IJavaScriptExecutor)Browser).ExecuteScript("return sessionStorage.getItem('suppress-enhanced-navigation');"); - throw new Exception($"Expected to use {expectedNavigation} because 'suppress-enhanced-navigation' is set to {isNavigationSupressed} but the element from previous path {isStale}"); + var isNavigationSuppressed = (string)((IJavaScriptExecutor)Browser).ExecuteScript("return sessionStorage.getItem('suppress-enhanced-navigation');"); + throw new Exception($"Expected to use {expectedNavigation} because 'suppress-enhanced-navigation' is set to {isNavigationSuppressed} but the element from previous path {isStale}. logging={logging}"); } private void AssertWeAreOnLandingPage() diff --git a/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs b/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs index 799b915f8fdd..1c3444f3a030 100644 --- a/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs +++ b/src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; using Microsoft.AspNetCore.E2ETesting; using OpenQA.Selenium; +using OpenQA.Selenium.BiDi.Communication; namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests; @@ -21,12 +22,20 @@ public static void SuppressEnhancedNavigation(ServerTestBase browser.Exists(By.TagName("h1")).Text); } - ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.setItem('suppress-enhanced-navigation', 'true')"); + // Set the suppression flag - this will prevent enhanced navigation from being attached on the next navigation + // and trigger detachment of currently attached enhanced navigation via the periodic check + var testId = Guid.NewGuid().ToString("N")[..8]; + ((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')"); + ((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('suppress-enhanced-navigation-{testId}', 'true')"); + + var suppressEnhancedNavigation = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('suppress-enhanced-navigation-{testId}');"); + Assert.True(suppressEnhancedNavigation is not null && (string)suppressEnhancedNavigation == "true", + "Expected 'suppress-enhanced-navigation' to be set in sessionStorage."); } } diff --git a/src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor b/src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor index 012dcc5547f8..7b8af1442eea 100644 --- a/src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor +++ b/src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor @@ -137,10 +137,10 @@ } const enableClassicInitializers = sessionStorage.getItem('enable-classic-initializers') === 'true'; - const suppressEnhancedNavigation = sessionStorage.getItem('suppress-enhanced-navigation') === 'true'; + const testId = sessionStorage.getItem('test-id'); + const suppressEnhancedNavigation = testId ? sessionStorage.getItem(`suppress-enhanced-navigation-${testId}`) === 'true' : false; const blockLoadBootResource = sessionStorage.getItem('block-load-boot-resource') === 'true'; const blockWebassemblySettings = sessionStorage.getItem('block-webassembly-settings') === 'true'; - sessionStorage.removeItem('suppress-enhanced-navigation'); sessionStorage.removeItem('block-load-boot-resource'); sessionStorage.removeItem('enable-classic-initializers'); sessionStorage.removeItem('block-webassembly-settings'); diff --git a/src/Components/test/testassets/Components.TestServer/RazorComponents/Root.razor b/src/Components/test/testassets/Components.TestServer/RazorComponents/Root.razor index 07a5daa32bde..098813212786 100644 --- a/src/Components/test/testassets/Components.TestServer/RazorComponents/Root.razor +++ b/src/Components/test/testassets/Components.TestServer/RazorComponents/Root.razor @@ -14,7 +14,8 @@