Skip to content

Commit 46df082

Browse files
committed
Reset browser window size in BrowserTestBase.DisposeAsync
1 parent 33edda9 commit 46df082

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

Lines changed: 10 additions & 8 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
}
@@ -44,12 +50,6 @@ public void NavMenuHighlightsCurrentLocation()
4450
var activeNavLinksSelector = By.CssSelector(".sidebar a.active");
4551
var mainHeaderSelector = By.TagName("h1");
4652

47-
// The sidebar is hidden if the screen is too narrow.
48-
// Without setting the window size explicitly, visibility-sensitive properties
49-
// such as IWebElement.Text can return empty strings, causing assertions to fail.
50-
// In particular, this happens in the headless mode (used when running without debugger).
51-
Browser.SetWindowSize(1920, 1080);
52-
5353
// Verify we start at home, with the home link highlighted
5454
Assert.Equal("Hello, world!", Browser.Exists(mainHeaderSelector).Text);
5555
Assert.Collection(Browser.FindElements(activeNavLinksSelector),
@@ -132,10 +132,12 @@ private void WaitUntilLoaded()
132132
Browser.NotEqual("Loading...", () => app.Text);
133133
}
134134

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

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 12 additions & 1 deletion
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
{
@@ -49,8 +50,13 @@ public IWebDriver Browser
4950

5051
public BrowserFixture BrowserFixture { get; }
5152

52-
public Task DisposeAsync()
53+
public virtual Task DisposeAsync()
5354
{
55+
if (_originalWindowSize.HasValue && _originalWindowSize != Browser.Manage().Window.Size)
56+
{
57+
Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height);
58+
}
59+
5460
return Task.CompletedTask;
5561
}
5662

@@ -78,6 +84,11 @@ protected void InitializeBrowser(string isolationContext)
7884
_asyncBrowser.Value = browser;
7985
_logs.Value = logs;
8086

87+
if (!_originalWindowSize.HasValue)
88+
{
89+
_originalWindowSize = browser.Manage().Window.Size;
90+
}
91+
8192
Browser = browser;
8293
}
8394
catch (Exception ex)

0 commit comments

Comments
 (0)