Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElementFocusComponent>();

// y scroll position before click
Expand Down Expand Up @@ -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<SvgFocusComponent>();

var buttonElement = appElement.FindElement(By.Id("focus-button"));
Expand All @@ -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<ElementFocusComponent>();

var buttonElement = Browser.Exists(By.Id("focus-button-prevented"));
Expand Down
12 changes: 10 additions & 2 deletions src/Components/test/E2ETest/Tests/StandaloneAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Microsoft.AspNetCore.Components.E2ETest.Tests;

public class StandaloneAppTest
: ServerTestBase<BlazorWasmTestAppFixture<StandaloneApp.Program>>, IDisposable
: ServerTestBase<BlazorWasmTestAppFixture<StandaloneApp.Program>>
{
public StandaloneAppTest(
BrowserFixture browserFixture,
Expand All @@ -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();
}
Expand Down Expand Up @@ -126,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();
}
}
21 changes: 20 additions & 1 deletion src/Shared/E2ETesting/BrowserTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class BrowserTestBase : IClassFixture<BrowserFixture>, IAsyncLifetime

private ExceptionDispatchInfo _exceptionDispatchInfo;
private IWebDriver _browser;
private System.Drawing.Size? _originalWindowSize;

public BrowserTestBase(BrowserFixture browserFixture, ITestOutputHelper output)
{
Expand Down Expand Up @@ -49,8 +50,21 @@ public IWebDriver Browser

public BrowserFixture BrowserFixture { get; }

public Task DisposeAsync()
public virtual Task DisposeAsync()
{
try
{
if (_originalWindowSize.HasValue && _originalWindowSize.Value != Browser.Manage().Window.Size)
{
Browser.SetWindowSize(_originalWindowSize.Value.Width, _originalWindowSize.Value.Height);
}
}
catch (WebDriverException)
{
// 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;
}

Expand Down Expand Up @@ -78,6 +92,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)
Expand Down
7 changes: 7 additions & 0 deletions src/Shared/E2ETesting/WebDriverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading