Skip to content

Commit d94b1af

Browse files
committed
Make the tests deterministic.
1 parent 0a60851 commit d94b1af

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/Components/test/E2ETest/Infrastructure/WebDriverExtensions/WebDriverExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using OpenQA.Selenium;
5+
using OpenQA.Selenium.Support.UI;
6+
using System;
57

68
namespace Microsoft.AspNetCore.Components.E2ETest;
79

@@ -14,4 +16,19 @@ public static void Navigate(this IWebDriver browser, Uri baseUri, string relativ
1416
browser.Navigate().GoToUrl("about:blank");
1517
browser.Navigate().GoToUrl(absoluteUrl);
1618
}
19+
20+
public static void WaitForElementToBeVisible(this IWebDriver browser, By by, int timeoutInSeconds = 5)
21+
{
22+
var wait = new DefaultWait<IWebDriver>(browser)
23+
{
24+
Timeout = TimeSpan.FromSeconds(timeoutInSeconds),
25+
PollingInterval = TimeSpan.FromMilliseconds(100)
26+
};
27+
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
28+
wait.Until(driver =>
29+
{
30+
var element = driver.FindElement(by);
31+
return element.Displayed;
32+
});
33+
}
1734
}

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Components.E2ETest;
45
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
56
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
67
using Microsoft.AspNetCore.E2ETesting;
@@ -668,7 +669,7 @@ public void CanUpdateHrefOnLinkTagWithIntegrity()
668669
[InlineData(false, true)]
669670
[InlineData(true, true)]
670671
[InlineData(true, false)]
671-
public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enableStreaming, bool useEnhancedNavigation)
672+
public void EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enableStreaming, bool useEnhancedNavigation)
672673
{
673674
// This test checks if the navigation to other path moves the scroll to the top of the page,
674675
// or to the beginning of a fragment, regardless of the previous scroll position,
@@ -683,6 +684,7 @@ public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enabl
683684
var elementForStalenessCheckOnScrollPage = Browser.Exists(By.TagName("html"));
684685

685686
var jsExecutor = (IJavaScriptExecutor)Browser;
687+
WaitFullPageLoaded();
686688
var maxScrollPosition = (long)jsExecutor.ExecuteScript("return document.documentElement.scrollHeight - window.innerHeight;");
687689

688690
// scroll maximally down and go to another page - we should land at the top of that page
@@ -693,11 +695,7 @@ public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enabl
693695
Assert.Equal(0, Browser.GetScrollY());
694696
var elementForStalenessCheckOnHashPage = Browser.Exists(By.TagName("html"));
695697

696-
if (enableStreaming)
697-
{
698-
// wait for the fragment to be visible - let the streaming finish
699-
await Task.Delay(TimeSpan.FromSeconds(1));
700-
}
698+
WaitFullPageLoaded();
701699
var fragmentScrollPosition = (long)jsExecutor.ExecuteScript("return Math.round(document.getElementById('some-content').getBoundingClientRect().top + window.scrollY);");
702700

703701
// go back and check if the scroll position is preserved
@@ -708,25 +706,15 @@ public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enabl
708706
// parts of page conditioned with showContent are showing with a delay - it affect the scroll position
709707
// from some reason, scroll position differs by 1 pixel between enhanced and browser's navigation
710708
var expectedMaxScrollPositionAfterBackwardsAction = useEnhancedNavigation ? maxScrollPosition: maxScrollPosition - 1;
711-
if (enableStreaming)
712-
{
713-
// let the streaming finish
714-
await Task.Delay(TimeSpan.FromSeconds(1));
715-
expectedMaxScrollPositionAfterBackwardsAction = maxScrollPosition + 127; // why 127 ???
716-
expectedMaxScrollPositionAfterBackwardsAction = useEnhancedNavigation ? expectedMaxScrollPositionAfterBackwardsAction : expectedMaxScrollPositionAfterBackwardsAction - 1;
717-
}
709+
WaitFullPageLoaded();
718710
Assert.Equal(expectedMaxScrollPositionAfterBackwardsAction, Browser.GetScrollY());
719711

720712
// navigate to a fragment on another page - we should land at the beginning of the fragment
721713
Browser.Exists(By.Id("do-navigation-with-fragment")).Click();
722714
AssertWeAreOnHashPage();
723715
AssertEnhancedNavigationOnHashPage();
724716
var expectedFragmentScrollPosition = fragmentScrollPosition - 1;
725-
if (enableStreaming)
726-
{
727-
// let the streaming finish
728-
await Task.Delay(TimeSpan.FromSeconds(1));
729-
}
717+
WaitFullPageLoaded();
730718
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
731719

732720
// go back to be able to go forward and check if the scroll position is preserved
@@ -737,6 +725,7 @@ public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enabl
737725
Browser.Navigate().Forward();
738726
AssertWeAreOnHashPage();
739727
AssertEnhancedNavigationOnHashPage();
728+
WaitFullPageLoaded();
740729
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
741730

742731
void AssertEnhancedNavigationOnHashPage() =>
@@ -765,6 +754,14 @@ void AssertWeAreOnHashPage()
765754
{
766755
Browser.Equal("Scroll to hash", () => Browser.Exists(By.Id("test-info")).Text);
767756
}
757+
758+
void WaitFullPageLoaded()
759+
{
760+
if (enableStreaming)
761+
{
762+
Browser.WaitForElementToBeVisible(By.Id("some-content"));
763+
}
764+
}
768765
}
769766

770767
private void AssertEnhancedUpdateCountEquals(long count)

0 commit comments

Comments
 (0)