From 33edda94362b01a01e048aa977021d23d02b4781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Mon, 8 Sep 2025 16:09:43 +0200 Subject: [PATCH 1/3] Fix NavMenuHighlightsCurrentLocation in headless mode by setting window size explictly, add SetWindowSize extension method --- .../test/E2ETest/Tests/ComponentRenderingTestBase.cs | 6 +++--- src/Components/test/E2ETest/Tests/StandaloneAppTest.cs | 6 ++++++ src/Shared/E2ETesting/WebDriverExtensions.cs | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs b/src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs index 92552b7ddc42..9c957c2da8a7 100644 --- a/src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs +++ b/src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs @@ -374,7 +374,7 @@ public void CanUseJsInteropToReferenceElements() [Fact] public void CanUseFocusExtensionToFocusElement() { - Browser.Manage().Window.Size = new System.Drawing.Size(100, 300); + Browser.SetWindowSize(100, 300); var appElement = Browser.MountTestComponent(); // y scroll position before click @@ -408,7 +408,7 @@ public void CanUseFocusExtensionToFocusElement() [Fact] public void CanUseFocusExtensionToFocusSvgElement() { - Browser.Manage().Window.Size = new System.Drawing.Size(100, 300); + Browser.SetWindowSize(100, 300); var appElement = Browser.MountTestComponent(); var buttonElement = appElement.FindElement(By.Id("focus-button")); @@ -430,7 +430,7 @@ public void CanUseFocusExtensionToFocusSvgElement() [Fact] public void CanUseFocusExtensionToFocusElementPreventScroll() { - Browser.Manage().Window.Size = new System.Drawing.Size(600, 600); + Browser.SetWindowSize(600, 600); var appElement = Browser.MountTestComponent(); var buttonElement = Browser.Exists(By.Id("focus-button-prevented")); diff --git a/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs b/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs index abdc012a2482..77cc0b969e27 100644 --- a/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs +++ b/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs @@ -44,6 +44,12 @@ public void NavMenuHighlightsCurrentLocation() var activeNavLinksSelector = By.CssSelector(".sidebar a.active"); var mainHeaderSelector = By.TagName("h1"); + // The sidebar is hidden if the screen is too narrow. + // Without setting the window size explicitly, visibility-sensitive properties + // such as IWebElement.Text can return empty strings, causing assertions to fail. + // In particular, this happens in the headless mode (used when running without debugger). + Browser.SetWindowSize(1920, 1080); + // Verify we start at home, with the home link highlighted Assert.Equal("Hello, world!", Browser.Exists(mainHeaderSelector).Text); Assert.Collection(Browser.FindElements(activeNavLinksSelector), diff --git a/src/Shared/E2ETesting/WebDriverExtensions.cs b/src/Shared/E2ETesting/WebDriverExtensions.cs index 50cbefdc9e3a..12a3b70f97ad 100644 --- a/src/Shared/E2ETesting/WebDriverExtensions.cs +++ b/src/Shared/E2ETesting/WebDriverExtensions.cs @@ -51,4 +51,11 @@ private static bool ShouldIgnore(LogEntry entry) return false; } + + public static void SetWindowSize(this IWebDriver driver, int width, int height) + { + ArgumentNullException.ThrowIfNull(driver); + + driver.Manage().Window.Size = new System.Drawing.Size(width, height); + } } From 46df082f89bfa2ab32de1422969c582e4c1242e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 9 Sep 2025 17:31:12 +0200 Subject: [PATCH 2/3] Reset browser window size in BrowserTestBase.DisposeAsync --- .../test/E2ETest/Tests/StandaloneAppTest.cs | 18 ++++++++++-------- src/Shared/E2ETesting/BrowserTestBase.cs | 13 ++++++++++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs b/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs index 77cc0b969e27..f5370b03a22b 100644 --- a/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs +++ b/src/Components/test/E2ETest/Tests/StandaloneAppTest.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests; public class StandaloneAppTest - : ServerTestBase>, IDisposable + : ServerTestBase> { public StandaloneAppTest( BrowserFixture browserFixture, @@ -22,6 +22,12 @@ public StandaloneAppTest( protected override void InitializeAsyncCore() { + // The sidebar is hidden if the screen is too narrow. + // Without setting the window size explicitly, visibility-sensitive properties (e.g. IWebElement.Text) + // and element finders (e.g. By.LinkText) can behave unexpectedly, causing assertions to fail. + // In particular, this happens in the headless mode (used when running without debugger). + Browser.SetWindowSize(1920, 1080); + Navigate("/"); WaitUntilLoaded(); } @@ -44,12 +50,6 @@ public void NavMenuHighlightsCurrentLocation() var activeNavLinksSelector = By.CssSelector(".sidebar a.active"); var mainHeaderSelector = By.TagName("h1"); - // The sidebar is hidden if the screen is too narrow. - // Without setting the window size explicitly, visibility-sensitive properties - // such as IWebElement.Text can return empty strings, causing assertions to fail. - // In particular, this happens in the headless mode (used when running without debugger). - Browser.SetWindowSize(1920, 1080); - // Verify we start at home, with the home link highlighted Assert.Equal("Hello, world!", Browser.Exists(mainHeaderSelector).Text); Assert.Collection(Browser.FindElements(activeNavLinksSelector), @@ -132,10 +132,12 @@ private void WaitUntilLoaded() Browser.NotEqual("Loading...", () => app.Text); } - public void Dispose() + public override Task DisposeAsync() { // Make the tests run faster by navigating back to the home page when we are done // If we don't, then the next test will reload the whole page before it starts Browser.Exists(By.LinkText("Home")).Click(); + + return base.DisposeAsync(); } } diff --git a/src/Shared/E2ETesting/BrowserTestBase.cs b/src/Shared/E2ETesting/BrowserTestBase.cs index 7a1927fe22cf..5dc25576516b 100644 --- a/src/Shared/E2ETesting/BrowserTestBase.cs +++ b/src/Shared/E2ETesting/BrowserTestBase.cs @@ -16,6 +16,7 @@ public class BrowserTestBase : IClassFixture, IAsyncLifetime private ExceptionDispatchInfo _exceptionDispatchInfo; private IWebDriver _browser; + private System.Drawing.Size? _originalWindowSize; public BrowserTestBase(BrowserFixture browserFixture, ITestOutputHelper output) { @@ -49,8 +50,13 @@ public IWebDriver Browser public BrowserFixture BrowserFixture { get; } - public Task DisposeAsync() + public virtual Task DisposeAsync() { + if (_originalWindowSize.HasValue && _originalWindowSize != Browser.Manage().Window.Size) + { + Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height); + } + return Task.CompletedTask; } @@ -78,6 +84,11 @@ protected void InitializeBrowser(string isolationContext) _asyncBrowser.Value = browser; _logs.Value = logs; + if (!_originalWindowSize.HasValue) + { + _originalWindowSize = browser.Manage().Window.Size; + } + Browser = browser; } catch (Exception ex) From 8706896395b3fa8995852c228b3f176e1153f7bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Wed, 10 Sep 2025 13:23:04 +0200 Subject: [PATCH 3/3] Catch WebDriverException when resetting window size of closed browser --- src/Shared/E2ETesting/BrowserTestBase.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Shared/E2ETesting/BrowserTestBase.cs b/src/Shared/E2ETesting/BrowserTestBase.cs index 5dc25576516b..a7e66a3169f9 100644 --- a/src/Shared/E2ETesting/BrowserTestBase.cs +++ b/src/Shared/E2ETesting/BrowserTestBase.cs @@ -52,9 +52,17 @@ public IWebDriver Browser public virtual Task DisposeAsync() { - if (_originalWindowSize.HasValue && _originalWindowSize != Browser.Manage().Window.Size) + try + { + if (_originalWindowSize.HasValue && _originalWindowSize.Value != Browser.Manage().Window.Size) + { + Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height); + } + } + catch (WebDriverException) { - Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height); + // An exception is thrown if the browser has been closed in the test. + // In that case we don't need to reset the window size. } return Task.CompletedTask;