Skip to content

Commit 9a1d687

Browse files
committed
More tests revealing a problem with forwards/backwards action.
1 parent bd7858d commit 9a1d687

File tree

4 files changed

+94
-56
lines changed

4 files changed

+94
-56
lines changed

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

Lines changed: 91 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -669,94 +669,132 @@ public void CanUpdateHrefOnLinkTagWithIntegrity()
669669
[InlineData(false, true)]
670670
[InlineData(true, true)]
671671
[InlineData(true, false)]
672-
public void EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enableStreaming, bool useEnhancedNavigation)
672+
public void EnhancedNavigationScrollBehavesSameAsBrowserOnNavLinkNavigation(bool enableStreaming, bool useEnhancedNavigation)
673673
{
674-
// This test checks if the navigation to other path moves the scroll to the top of the page,
675-
// or to the beginning of a fragment, regardless of the previous scroll position,
676-
// checks if going backwards and forwards preserves the scroll position
674+
// This test checks if the navigation to another path moves the scroll to the top of the page,
675+
// or to the beginning of a fragment, regardless of the previous scroll position
677676
string landingPageSuffix = enableStreaming ? "" : "-no-streaming";
678677
Navigate($"{ServerPathBase}/nav/testing-scroll{landingPageSuffix}");
679678
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation, skipNavigation: true);
680679

680+
// "scroll" page: scroll maximally down and go to "hash" page - we should land at the top of that page
681681
AssertWeAreOnScrollTestPage();
682+
WaitStreamingRendersFullPage(enableStreaming);
682683

683-
// assert enhanced navigation is enabled/disabled, as requested
684-
var elementForStalenessCheckOnScrollPage = Browser.Exists(By.TagName("html"));
684+
// staleness check is used to assert enhanced navigation is enabled/disabled, as requested
685+
var elementForStalenessCheckOnHashPage = Browser.Exists(By.TagName("html"));
685686

686687
var jsExecutor = (IJavaScriptExecutor)Browser;
687-
WaitFullPageLoaded();
688688
var maxScrollPosition = (long)jsExecutor.ExecuteScript("return document.documentElement.scrollHeight - window.innerHeight;");
689-
690-
// scroll maximally down and go to another page - we should land at the top of that page
691689
Browser.SetScrollY(maxScrollPosition);
692690
Browser.Exists(By.Id("do-navigation")).Click();
693-
WaitFullPageLoaded();
694-
AssertEnhancedNavigationOnHashPage();
691+
692+
// "hash" page: check if we landed at 0, then navigate to "scroll"
693+
WaitStreamingRendersFullPage(enableStreaming);
694+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnHashPage);
695695
AssertWeAreOnHashPage();
696696
Assert.Equal(0, Browser.GetScrollY());
697-
var elementForStalenessCheckOnHashPage = Browser.Exists(By.TagName("html"));
698-
697+
var elementForStalenessCheckOnScrollPage = Browser.Exists(By.TagName("html"));
699698
var fragmentScrollPosition = (long)jsExecutor.ExecuteScript("return Math.round(document.getElementById('some-content').getBoundingClientRect().top + window.scrollY);");
699+
Browser.Exists(By.Id("do-navigation")).Click();
700700

701-
// go back and check if the scroll position is preserved
702-
Browser.Navigate().Back();
701+
// "scroll" page: navigate to a fragment on another page - we should land at the beginning of the fragment
703702
AssertWeAreOnScrollTestPage();
704-
WaitFullPageLoaded();
705-
AssertEnhancedNavigationOnScrollPage();
703+
WaitStreamingRendersFullPage(enableStreaming);
704+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnScrollPage);
706705

707-
// parts of page conditioned with showContent are showing with a delay - it affect the scroll position
708-
// from some reason, scroll position differs by 1 pixel between enhanced and browser's navigation
709-
var expectedMaxScrollPositionAfterBackwardsAction = useEnhancedNavigation ? maxScrollPosition: maxScrollPosition - 1;
710-
Assert.Equal(expectedMaxScrollPositionAfterBackwardsAction, Browser.GetScrollY());
711-
712-
// navigate to a fragment on another page - we should land at the beginning of the fragment
713706
Browser.Exists(By.Id("do-navigation-with-fragment")).Click();
714707
AssertWeAreOnHashPage();
715-
WaitFullPageLoaded();
716-
AssertEnhancedNavigationOnHashPage();
708+
WaitStreamingRendersFullPage(enableStreaming);
709+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnHashPage);
717710
var expectedFragmentScrollPosition = fragmentScrollPosition - 1;
718711
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
712+
}
713+
714+
[Theory]
715+
[InlineData(false, false)]
716+
[InlineData(false, true)] // ToDo: Forwards -> line 772, Expected: 3270 Actual: 1400
717+
[InlineData(true, true)] // ToDo: Forwards -> line 772, Expected: 2400 Actual: 412
718+
[InlineData(true, false)] // ToDo: why it requires a special condition?
719+
public void EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(bool enableStreaming, bool useEnhancedNavigation)
720+
{
721+
// This test checks if the scroll position is preserved after backwards/forwards action
722+
string landingPageSuffix = enableStreaming ? "" : "-no-streaming";
723+
Navigate($"{ServerPathBase}/nav/testing-scroll{landingPageSuffix}");
724+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation, skipNavigation: true);
725+
726+
// "scroll" page: max scroll position on exiting it
727+
AssertWeAreOnScrollTestPage();
728+
WaitStreamingRendersFullPage(enableStreaming);
729+
730+
// staleness check is used to assert enhanced navigation is enabled/disabled, as requested
731+
var elementForStalenessCheckOnHashPage = Browser.Exists(By.TagName("html"));
719732

720-
// go back to be able to go forward and check if the scroll position is preserved
733+
var jsExecutor = (IJavaScriptExecutor)Browser;
734+
var maxScrollPosition = (long)jsExecutor.ExecuteScript("return document.documentElement.scrollHeight - window.innerHeight;");
735+
Browser.SetScrollY(maxScrollPosition);
736+
Browser.Exists(By.Id("do-navigation")).Click();
737+
738+
// "hash" page: scroll position at fragment on exiting it
739+
WaitStreamingRendersFullPage(enableStreaming);
740+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnHashPage);
741+
AssertWeAreOnHashPage();
742+
var elementForStalenessCheckOnScrollPage = Browser.Exists(By.TagName("html"));
743+
var fragmentScrollPosition = (long)jsExecutor.ExecuteScript("return Math.round(document.getElementById('some-content').getBoundingClientRect().top + window.scrollY);");
744+
Browser.SetScrollY(fragmentScrollPosition);
721745
Browser.Navigate().Back();
746+
747+
// "scroll" page: check if the scroll position is preserved at the bottom
722748
AssertWeAreOnScrollTestPage();
723-
WaitFullPageLoaded();
724-
AssertEnhancedNavigationOnScrollPage();
749+
WaitStreamingRendersFullPage(enableStreaming);
750+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnScrollPage);
751+
752+
// from some reason, scroll position differs by 1 pixel between enhanced and browser's navigation
753+
// browser's navigation is not precisely going backwards/forwards to the previous state
754+
var expectedMaxScrollPositionAfterBackwardsAction = useEnhancedNavigation ? maxScrollPosition : maxScrollPosition - 1;
755+
Assert.Equal(expectedMaxScrollPositionAfterBackwardsAction, Browser.GetScrollY());
725756

757+
// "scroll" page: go forwards to "hash" page, max scroll position on exiting
726758
Browser.Navigate().Forward();
727759
AssertWeAreOnHashPage();
728-
WaitFullPageLoaded();
729-
AssertEnhancedNavigationOnHashPage();
760+
WaitStreamingRendersFullPage(enableStreaming);
761+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnHashPage);
762+
// WHY is streaming with browser same as non-streaming with enhanced nav but different than non-streaming with browser?
763+
var expectedFragmentScrollPosition = (useEnhancedNavigation || enableStreaming) ? fragmentScrollPosition : fragmentScrollPosition - 1;
730764
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
731765

732-
void AssertEnhancedNavigationOnHashPage() =>
733-
AssertEnhancedNavigation(elementForStalenessCheckOnScrollPage);
734-
735-
void AssertEnhancedNavigationOnScrollPage()
736-
=> AssertEnhancedNavigation(elementForStalenessCheckOnHashPage);
766+
// "hash" page: go back to "scroll" page
767+
Browser.Navigate().Back();
768+
AssertWeAreOnScrollTestPage();
769+
WaitStreamingRendersFullPage(enableStreaming);
770+
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnScrollPage);
771+
var expectedMaxScrollPositionAfterSecondBackwardsAction = useEnhancedNavigation
772+
? expectedMaxScrollPositionAfterBackwardsAction
773+
: expectedMaxScrollPositionAfterBackwardsAction - 1;
774+
Assert.Equal(expectedMaxScrollPositionAfterSecondBackwardsAction, Browser.GetScrollY());
775+
}
737776

738-
void AssertEnhancedNavigation(IWebElement elementForStalenessCheck)
739-
{
740-
bool enhancedNavigationDetected = !IsElementStale(elementForStalenessCheck);
741-
Assert.Equal(useEnhancedNavigation, enhancedNavigationDetected);
742-
}
777+
private void AssertEnhancedNavigation(bool useEnhancedNavigation, IWebElement elementForStalenessCheck)
778+
{
779+
bool enhancedNavigationDetected = !IsElementStale(elementForStalenessCheck);
780+
Assert.Equal(useEnhancedNavigation, enhancedNavigationDetected);
781+
}
743782

744-
void AssertWeAreOnScrollTestPage()
745-
{
746-
Browser.Equal("Go back to me", () => Browser.Exists(By.Id("test-info")).Text);
747-
}
783+
private void AssertWeAreOnScrollTestPage()
784+
{
785+
Browser.Equal("Scroll tests landing page", () => Browser.Exists(By.Id("test-info")).Text);
786+
}
748787

749-
void AssertWeAreOnHashPage()
750-
{
751-
Browser.Equal("Scroll to hash", () => Browser.Exists(By.Id("test-info")).Text);
752-
}
788+
private void AssertWeAreOnHashPage()
789+
{
790+
Browser.Equal("Scroll to hash", () => Browser.Exists(By.Id("test-info")).Text);
791+
}
753792

754-
void WaitFullPageLoaded()
793+
private void WaitStreamingRendersFullPage(bool enableStreaming)
794+
{
795+
if (enableStreaming)
755796
{
756-
if (enableStreaming)
757-
{
758-
Browser.WaitForElementToBeVisible(By.Id("some-content"));
759-
}
797+
Browser.WaitForElementToBeVisible(By.Id("some-content"));
760798
}
761799
}
762800

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTests.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<PageTitle>Page for testing scroll position</PageTitle>
66

7-
<h1 id="test-info">Go back to me</h1>
7+
<h1 id="test-info">Scroll tests landing page</h1>
88

99
<p>If you scroll down a long way, you'll find more content. We add it asynchronously via streaming rendering.</p>
1010

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTestsNoStreaming.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<PageTitle>Page for testing scroll position</PageTitle>
66

7-
<h1 id="test-info">Go back to me</h1>
7+
<h1 id="test-info">Scroll tests landing page</h1>
88

99
<p>If you scroll down a long way, you'll find more content.</p>
1010

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollingToHashNoStreaming.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div id="uri-on-page-load" style="display: none" data-value="@uriOnPageLoad"></div>
1414
</p>
1515

16-
<div style="height: 2000px; border: 2px dashed red;">top spacer</div>
16+
<div style="height: 1000px; border: 2px dashed red;">top spacer</div>
1717

1818
@if (showContent)
1919
{

0 commit comments

Comments
 (0)