Skip to content

Commit abbcda2

Browse files
authored
Fix NavMenuHighlightsCurrentLocation in headless mode (#63583)
1 parent 641f0b4 commit abbcda2

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void CanUseJsInteropToReferenceElements()
374374
[Fact]
375375
public void CanUseFocusExtensionToFocusElement()
376376
{
377-
Browser.Manage().Window.Size = new System.Drawing.Size(100, 300);
377+
Browser.SetWindowSize(100, 300);
378378
var appElement = Browser.MountTestComponent<ElementFocusComponent>();
379379

380380
// y scroll position before click
@@ -408,7 +408,7 @@ public void CanUseFocusExtensionToFocusElement()
408408
[Fact]
409409
public void CanUseFocusExtensionToFocusSvgElement()
410410
{
411-
Browser.Manage().Window.Size = new System.Drawing.Size(100, 300);
411+
Browser.SetWindowSize(100, 300);
412412
var appElement = Browser.MountTestComponent<SvgFocusComponent>();
413413

414414
var buttonElement = appElement.FindElement(By.Id("focus-button"));
@@ -430,7 +430,7 @@ public void CanUseFocusExtensionToFocusSvgElement()
430430
[Fact]
431431
public void CanUseFocusExtensionToFocusElementPreventScroll()
432432
{
433-
Browser.Manage().Window.Size = new System.Drawing.Size(600, 600);
433+
Browser.SetWindowSize(600, 600);
434434
var appElement = Browser.MountTestComponent<ElementFocusComponent>();
435435

436436
var buttonElement = Browser.Exists(By.Id("focus-button-prevented"));

src/Components/test/E2ETest/Tests/StandaloneAppTest.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Microsoft.AspNetCore.Components.E2ETest.Tests;
1111

1212
public class StandaloneAppTest
13-
: ServerTestBase<BlazorWasmTestAppFixture<StandaloneApp.Program>>, IDisposable
13+
: ServerTestBase<BlazorWasmTestAppFixture<StandaloneApp.Program>>
1414
{
1515
public StandaloneAppTest(
1616
BrowserFixture browserFixture,
@@ -22,6 +22,12 @@ public StandaloneAppTest(
2222

2323
protected override void InitializeAsyncCore()
2424
{
25+
// The sidebar is hidden if the screen is too narrow.
26+
// Without setting the window size explicitly, visibility-sensitive properties (e.g. IWebElement.Text)
27+
// and element finders (e.g. By.LinkText) can behave unexpectedly, causing assertions to fail.
28+
// In particular, this happens in the headless mode (used when running without debugger).
29+
Browser.SetWindowSize(1920, 1080);
30+
2531
Navigate("/");
2632
WaitUntilLoaded();
2733
}
@@ -126,10 +132,12 @@ private void WaitUntilLoaded()
126132
Browser.NotEqual("Loading...", () => app.Text);
127133
}
128134

129-
public void Dispose()
135+
public override Task DisposeAsync()
130136
{
131137
// Make the tests run faster by navigating back to the home page when we are done
132138
// If we don't, then the next test will reload the whole page before it starts
133139
Browser.Exists(By.LinkText("Home")).Click();
140+
141+
return base.DisposeAsync();
134142
}
135143
}

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class BrowserTestBase : IClassFixture<BrowserFixture>, IAsyncLifetime
1616

1717
private ExceptionDispatchInfo _exceptionDispatchInfo;
1818
private IWebDriver _browser;
19+
private System.Drawing.Size? _originalWindowSize;
1920

2021
public BrowserTestBase(BrowserFixture browserFixture, ITestOutputHelper output)
2122
{
@@ -51,6 +52,19 @@ public IWebDriver Browser
5152

5253
public virtual Task DisposeAsync()
5354
{
55+
try
56+
{
57+
if (_originalWindowSize.HasValue && _originalWindowSize.Value != Browser.Manage().Window.Size)
58+
{
59+
Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height);
60+
}
61+
}
62+
catch (WebDriverException)
63+
{
64+
// An exception is thrown if the browser has been closed in the test.
65+
// In that case we don't need to reset the window size.
66+
}
67+
5468
return Task.CompletedTask;
5569
}
5670

@@ -78,6 +92,11 @@ protected void InitializeBrowser(string isolationContext)
7892
_asyncBrowser.Value = browser;
7993
_logs.Value = logs;
8094

95+
if (!_originalWindowSize.HasValue)
96+
{
97+
_originalWindowSize = browser.Manage().Window.Size;
98+
}
99+
81100
Browser = browser;
82101
}
83102
catch (Exception ex)

src/Shared/E2ETesting/WebDriverExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ private static bool ShouldIgnore(LogEntry entry)
5151

5252
return false;
5353
}
54+
55+
public static void SetWindowSize(this IWebDriver driver, int width, int height)
56+
{
57+
ArgumentNullException.ThrowIfNull(driver);
58+
59+
driver.Manage().Window.Size = new System.Drawing.Size(width, height);
60+
}
5461
}

0 commit comments

Comments
 (0)