-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathEnhancedNavigationTest.cs
More file actions
1026 lines (831 loc) · 48.9 KB
/
EnhancedNavigationTest.cs
File metadata and controls
1026 lines (831 loc) · 48.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Globalization;
using System.Threading.Tasks;
using Components.TestServer.RazorComponents;
using Microsoft.AspNetCore.Components.E2ETest;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using Microsoft.AspNetCore.InternalTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.BiDi;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.Support.Extensions;
using TestServer;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
[CollectionDefinition(nameof(EnhancedNavigationTest), DisableParallelization = true)]
public class EnhancedNavigationTest : ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>>
{
public EnhancedNavigationTest(
BrowserFixture browserFixture,
BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture,
ITestOutputHelper output)
: base(browserFixture, serverFixture, output)
{
}
// One of the tests here makes use of the streaming rendering page, which uses global state
// so we can't run at the same time as other such tests
public override Task InitializeAsync()
=> InitializeAsync(BrowserFixture.StreamingContext);
[Fact]
public void CanNavigateToAnotherPageWhilePreservingCommonDOMElements()
{
Navigate($"{ServerPathBase}/nav");
var h1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => h1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Streaming")).Click();
// Important: we're checking the *same* <h1> element as earlier, showing that we got to the
// destination, and it's done so without a page load, and it preserved the element
Browser.Equal("Streaming Rendering", () => h1Elem.Text);
// We have to make the response finish otherwise the test will fail when it tries to dispose the server
Browser.FindElement(By.Id("end-response-link")).Click();
}
[Fact]
public void CanNavigateToAnHtmlPageWithAnErrorStatus()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Error page with 404 content")).Click();
Browser.Equal("404", () => Browser.Exists(By.TagName("h1")).Text);
}
[Fact]
public void DisplaysStatusCodeIfResponseIsErrorWithNoContent()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Error page with no content")).Click();
Browser.Equal("Error: 404 Not Found", () => Browser.Exists(By.TagName("html")).Text);
}
[Fact]
public void CanNavigateToNonHtmlResponse()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Non-HTML page")).Click();
Browser.Equal("Hello, this is plain text", () => Browser.Exists(By.TagName("html")).Text);
//Check if the fall back because of the non-html response sends a warning
var logs = Browser.GetBrowserLogs(LogLevel.Warning);
Assert.Contains(logs, log => log.Message.Contains("Enhanced navigation failed for destination") && log.Message.Contains("Falling back to full page load.") && !log.Message.Contains("Error"));
}
[Fact]
public void EnhancedNavRequestsIncludeExpectedHeaders()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("List headers")).Click();
var ul = Browser.Exists(By.Id("all-headers"));
var allHeaders = ul.FindElements(By.TagName("li")).Select(x => x.Text.ToLowerInvariant()).ToList();
// Specifying text/html is to make the enhanced nav outcomes more similar to non-enhanced nav.
// For example, the default error middleware will only serve the error page if this content type is requested.
// The blazor-enhanced-nav parameter can be used to trigger arbitrary server-side behaviors.
Assert.Contains("accept: text/html; blazor-enhanced-nav=on", allHeaders);
}
[Fact]
public void EnhancedNavCanBeDisabledHierarchically()
{
Navigate($"{ServerPathBase}/nav");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => originalH1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.Id("not-enhanced-nav-link")).Click();
// Check we got there, but we did *not* retain the <h1> element
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
}
[Fact]
public void EnhancedNavCanBeReenabledHierarchically()
{
Navigate($"{ServerPathBase}/nav");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => originalH1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Other (re-enabled enhanced nav)")).Click();
// Check we got there, and it did retain the <h1> element
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Equal("Other", originalH1Elem.Text);
}
[Fact]
public void EnhancedNavWorksInsideSVGElement()
{
Navigate($"{ServerPathBase}/nav");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => originalH1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.Id("svg-nav-link")).Click();
// Check we got there, and it did retain the <h1> element
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Equal("Other", originalH1Elem.Text);
}
[Fact]
public void EnhancedNavCanBeDisabledInSVGElementContainingAnchor()
{
Navigate($"{ServerPathBase}/nav");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => originalH1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.Id("svg-not-enhanced-nav-link")).Click();
// Check we got there, but we did *not* retain the <h1> element
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
}
[Fact]
public void EnhancedNavCanBeDisabledInSVGElementInsideAnchor()
{
Navigate($"{ServerPathBase}/nav");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("Hello", () => originalH1Elem.Text);
Browser.Exists(By.TagName("nav")).FindElement(By.Id("svg-in-anchor-not-enhanced-nav-link")).Click();
// Check we got there, but we did *not* retain the <h1> element
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
}
[Fact]
public void ScrollsToHashWithContentAddedAsynchronously()
{
Navigate($"{ServerPathBase}/nav");
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Scroll to hash")).Click();
Assert.Equal(0, Browser.GetScrollY());
var asyncContentHeader = Browser.Exists(By.Id("some-content"));
Browser.Equal("Some content", () => asyncContentHeader.Text);
Browser.True(() => Browser.GetScrollY() > 500);
}
[Fact]
public void CanScrollToHashWithoutPerformingFullNavigation()
{
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
Browser.Equal("Scroll to hash", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.Id("scroll-anchor")).Click();
Browser.True(() => Browser.GetScrollY() > 500);
Browser.True(() => Browser
.Exists(By.Id("uri-on-page-load"))
.GetDomAttribute("data-value")
.EndsWith("scroll-to-hash", StringComparison.Ordinal));
}
[Fact]
public void NonEnhancedNavCanScrollToHashWithoutFetchingPageAnchor()
{
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
var originalTextElem = Browser.Exists(By.CssSelector("#anchor #text"));
Browser.Equal("Text", () => originalTextElem.Text);
Browser.Exists(By.CssSelector("#anchor #scroll-anchor")).Click();
Browser.True(() => Browser.GetScrollY() > 500);
Browser.True(() => Browser
.Exists(By.CssSelector("#anchor #uri-on-page-load"))
.GetDomAttribute("data-value")
.EndsWith("scroll-to-hash", StringComparison.Ordinal));
Browser.Equal("Text", () => originalTextElem.Text);
}
[Fact]
public void NonEnhancedNavCanScrollToHashWithoutFetchingPageNavLink()
{
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
var originalTextElem = Browser.Exists(By.CssSelector("#navlink #text"));
Browser.Equal("Text", () => originalTextElem.Text);
Browser.Exists(By.CssSelector("#navlink #scroll-anchor")).Click();
Browser.True(() => Browser.GetScrollY() > 500);
Browser.True(() => Browser
.Exists(By.CssSelector("#navlink #uri-on-page-load"))
.GetDomAttribute("data-value")
.EndsWith("scroll-to-hash", StringComparison.Ordinal));
Browser.Equal("Text", () => originalTextElem.Text);
}
[Theory]
[InlineData("server")]
[InlineData("webassembly")]
public void CanPerformProgrammaticEnhancedNavigation(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
// Normally, you shouldn't store references to elements because they could become stale references
// after the page re-renders. However, we want to explicitly test that the element persists across
// renders to ensure that enhanced navigation occurs instead of a full page reload.
// Here, we pick an element that we know will persist across navigations so we can check
// for its staleness.
var elementForStalenessCheck = Browser.Exists(By.TagName("html"));
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
Browser.False(() => elementForStalenessCheck.IsStale());
Browser.Exists(By.Id("navigate-to-another-page")).Click();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
Browser.False(() => elementForStalenessCheck.IsStale());
// Ensure that the history stack was correctly updated
Browser.Navigate().Back();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
Browser.False(() => elementForStalenessCheck.IsStale());
Browser.Navigate().Back();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
Browser.False(() => elementForStalenessCheck.IsStale());
}
[Theory]
[InlineData("server", "refresh-with-navigate-to")]
[InlineData("webassembly", "refresh-with-navigate-to")]
[InlineData("server", "refresh-with-refresh")]
[InlineData("webassembly", "refresh-with-refresh")]
public void CanPerformProgrammaticEnhancedRefresh(string renderMode, string refreshButtonId)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
// Normally, you shouldn't store references to elements because they could become stale references
// after the page re-renders. However, we want to explicitly test that the element persists across
// renders to ensure that enhanced navigation occurs instead of a full page reload.
var renderIdElement = Browser.Exists(By.Id("render-id"));
var initialRenderId = -1;
Browser.True(() => int.TryParse(renderIdElement.Text, out initialRenderId));
Assert.NotEqual(-1, initialRenderId);
Browser.Exists(By.Id(refreshButtonId)).Click();
Browser.True(() =>
{
if (renderIdElement.IsStale() || !int.TryParse(renderIdElement.Text, out var newRenderId))
{
return false;
}
return newRenderId > initialRenderId;
});
// Ensure that the history stack was correctly updated
Browser.Navigate().Back();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
}
[Theory]
[InlineData("server")]
[InlineData("webassembly")]
public void NavigateToCanFallBackOnFullPageReload(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
// Normally, you shouldn't store references to elements because they could become stale references
// after the page re-renders. However, we want to explicitly test that the element becomes stale
// across renders to ensure that a full page reload occurs.
var initialRenderIdElement = Browser.Exists(By.Id("render-id"));
var initialRenderId = -1;
Browser.True(() => int.TryParse(initialRenderIdElement.Text, out initialRenderId));
Assert.NotEqual(-1, initialRenderId);
Browser.Exists(By.Id("reload-with-navigate-to")).Click();
Browser.True(() => initialRenderIdElement.IsStale());
var finalRenderIdElement = Browser.Exists(By.Id("render-id"));
var finalRenderId = -1;
Browser.True(() => int.TryParse(finalRenderIdElement.Text, out finalRenderId));
Assert.NotEqual(-1, initialRenderId);
Assert.True(finalRenderId > initialRenderId);
// Ensure that the history stack was correctly updated
Browser.Navigate().Back();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
}
[Theory]
[InlineData("server")]
[InlineData("webassembly")]
public void RefreshCanFallBackOnFullPageReload(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true, skipNavigation: true);
Browser.Navigate().Refresh();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
// if we don't clean up the suppression, all subsequent navigations will be suppressed by default
EnhancedNavigationTestUtil.CleanEnhancedNavigationSuppression(this, skipNavigation: true);
// Normally, you shouldn't store references to elements because they could become stale references
// after the page re-renders. However, we want to explicitly test that the element becomes stale
// across renders to ensure that a full page reload occurs.
var initialRenderIdElement = Browser.Exists(By.Id("render-id"));
var initialRenderId = -1;
Browser.True(() => int.TryParse(initialRenderIdElement.Text, out initialRenderId));
Assert.NotEqual(-1, initialRenderId);
Browser.Exists(By.Id("refresh-with-refresh")).Click();
Browser.True(() => initialRenderIdElement.IsStale());
var finalRenderIdElement = Browser.Exists(By.Id("render-id"));
var finalRenderId = -1;
Browser.True(() => int.TryParse(finalRenderIdElement.Text, out finalRenderId));
Assert.NotEqual(-1, initialRenderId);
Assert.True(finalRenderId > initialRenderId);
// Ensure that the history stack was correctly updated
Browser.Navigate().Back();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
}
[Theory]
[InlineData("server")]
[InlineData("webassembly")]
public void RefreshWithForceReloadDoesFullPageReload(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
// Normally, you shouldn't store references to elements because they could become stale references
// after the page re-renders. However, we want to explicitly test that the element becomes stale
// across renders to ensure that a full page reload occurs.
var initialRenderIdElement = Browser.Exists(By.Id("render-id"));
var initialRenderId = -1;
Browser.True(() => int.TryParse(initialRenderIdElement.Text, out initialRenderId));
Assert.NotEqual(-1, initialRenderId);
Browser.Exists(By.Id("reload-with-refresh")).Click();
Browser.True(() => initialRenderIdElement.IsStale());
var finalRenderIdElement = Browser.Exists(By.Id("render-id"));
var finalRenderId = -1;
Browser.True(() => int.TryParse(finalRenderIdElement.Text, out finalRenderId));
Assert.NotEqual(-1, initialRenderId);
Assert.True(finalRenderId > initialRenderId);
// Ensure that the history stack was correctly updated
Browser.Navigate().Back();
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav", Browser.Url);
}
[Fact]
public void CanRegisterAndRemoveEnhancedPageUpdateCallback()
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Preserve content")).Click();
Browser.Equal("Page that preserves content", () => Browser.Exists(By.TagName("h1")).Text);
// Required until https://github.com/dotnet/aspnetcore/issues/50424 is fixed
Browser.Navigate().Refresh();
Browser.Exists(By.Id("refresh-with-refresh"));
Browser.Click(By.Id("start-listening"));
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(1);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(2);
Browser.Click(By.Id("stop-listening"));
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(2);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(2);
void AssertEnhancedUpdateCountEquals(long count)
=> Browser.Equal(count, () => ((IJavaScriptExecutor)Browser).ExecuteScript("return window.enhancedPageUpdateCount;"));
}
[Fact]
public void ElementsWithDataPermanentAttribute_HavePreservedContent()
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Preserve content")).Click();
Browser.Equal("Page that preserves content", () => Browser.Exists(By.TagName("h1")).Text);
// Required until https://github.com/dotnet/aspnetcore/issues/50424 is fixed
Browser.Navigate().Refresh();
Browser.Exists(By.Id("refresh-with-refresh"));
Browser.Click(By.Id("start-listening"));
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(1);
Browser.Equal("Preserved content", () => Browser.Exists(By.Id("preserved-content")).Text);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(2);
Browser.Equal("Preserved content", () => Browser.Exists(By.Id("preserved-content")).Text);
}
[Fact]
public void ElementsWithoutDataPermanentAttribute_DoNotHavePreservedContent()
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Preserve content")).Click();
Browser.Equal("Page that preserves content", () => Browser.Exists(By.TagName("h1")).Text);
// Required until https://github.com/dotnet/aspnetcore/issues/50424 is fixed
Browser.Navigate().Refresh();
Browser.Exists(By.Id("refresh-with-refresh"));
Browser.Click(By.Id("start-listening"));
Browser.Equal("Non preserved content", () => Browser.Exists(By.Id("non-preserved-content")).Text);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(1);
Browser.Equal("", () => Browser.Exists(By.Id("non-preserved-content")).Text);
}
[Fact]
public void ElementsWithDataPermanentAttribute_HavePreservedAttributes()
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Preserve content")).Click();
Browser.Equal("Page that preserves content", () => Browser.Exists(By.TagName("h1")).Text);
// Required until https://github.com/dotnet/aspnetcore/issues/50424 is fixed
Browser.Navigate().Refresh();
Browser.Exists(By.Id("refresh-with-refresh"));
Browser.Click(By.Id("start-listening"));
// Verify the dynamically added class exists before enhanced nav
var preservedAttributesElement = Browser.Exists(By.Id("preserved-attributes"));
Browser.True(() => preservedAttributesElement.GetAttribute("class")?.Contains("dynamically-added-class") == true);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(1);
// Verify the dynamically added class is preserved after enhanced nav
Browser.True(() => preservedAttributesElement.GetAttribute("class")?.Contains("dynamically-added-class") == true);
Browser.Click(By.Id("refresh-with-refresh"));
AssertEnhancedUpdateCountEquals(2);
// Verify the dynamically added class is still preserved after another enhanced nav
Browser.True(() => preservedAttributesElement.GetAttribute("class")?.Contains("dynamically-added-class") == true);
}
[Fact]
public void EnhancedNavNotUsedForNonBlazorDestinations()
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Equal("object", Browser.ExecuteJavaScript<string>("return typeof Blazor")); // Blazor JS is loaded
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("Non-Blazor HTML page")).Click();
Browser.Equal("This is a non-Blazor endpoint", () => Browser.Exists(By.TagName("h1")).Text);
Assert.Equal("undefined", Browser.ExecuteJavaScript<string>("return typeof Blazor")); // Blazor JS is NOT loaded
//Check if the fall back because of the non-blazor endpoint navigation sends a warning
var logs = Browser.GetBrowserLogs(LogLevel.Warning);
Assert.Contains(logs, log => log.Message.Contains("Enhanced navigation failed for destination") && log.Message.Contains("Falling back to full page load.") && !log.Message.Contains("Error"));
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void LocationChangedEventGetsInvokedOnEnhancedNavigation_OnlyServerOrWebAssembly(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"LocationChanged/LocationChanging event ({renderMode})")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id($"location-changed-count-{renderMode}")).Text);
Browser.Exists(By.Id($"update-query-string-{renderMode}")).Click();
Browser.Equal("1", () => Browser.Exists(By.Id($"location-changed-count-{renderMode}")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void LocationChangedEventGetsInvokedOnEnhancedNavigation_BothServerAndWebAssembly(string runtimeThatInvokedNavigation)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("LocationChanged/LocationChanging event (server-and-wasm)")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id("location-changed-count-server")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id("location-changed-count-wasm")).Text);
Browser.Exists(By.Id($"update-query-string-{runtimeThatInvokedNavigation}")).Click();
// LocationChanged event gets invoked for both interactive runtimes
Browser.Equal("1", () => Browser.Exists(By.Id("location-changed-count-server")).Text);
Browser.Equal("1", () => Browser.Exists(By.Id("location-changed-count-wasm")).Text);
}
[Theory]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/66310")]
[InlineData("server")]
[InlineData("wasm")]
public void NavigationManagerUriGetsUpdatedOnEnhancedNavigation_OnlyServerOrWebAssembly(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"LocationChanged/LocationChanging event ({renderMode})")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith($"/nav/location-changed/{renderMode}", Browser.Exists(By.Id($"nav-uri-{renderMode}")).Text);
Browser.Exists(By.Id($"update-query-string-{renderMode}")).Click();
Assert.EndsWith($"/nav/location-changed/{renderMode}?query=1", Browser.Exists(By.Id($"nav-uri-{renderMode}")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void NavigationManagerUriGetsUpdatedOnEnhancedNavigation_BothServerAndWebAssembly(string runtimeThatInvokedNavigation)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("LocationChanged/LocationChanging event (server-and-wasm)")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Assert.EndsWith("/nav/location-changed/server-and-wasm", Browser.Exists(By.Id("nav-uri-server")).Text);
Assert.EndsWith("/nav/location-changed/server-and-wasm", Browser.Exists(By.Id("nav-uri-wasm")).Text);
Browser.Exists(By.Id($"update-query-string-{runtimeThatInvokedNavigation}")).Click();
Assert.EndsWith($"/nav/location-changed/server-and-wasm?query=1", Browser.Exists(By.Id($"nav-uri-server")).Text);
Assert.EndsWith($"/nav/location-changed/server-and-wasm?query=1", Browser.Exists(By.Id($"nav-uri-wasm")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void SupplyParameterFromQueryGetsUpdatedOnEnhancedNavigation_OnlyServerOrWebAssembly(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"LocationChanged/LocationChanging event ({renderMode})")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.Id($"update-query-string-{renderMode}")).Click();
Browser.Equal("1", () => Browser.Exists(By.Id($"query-{renderMode}")).Text);
Browser.Exists(By.Id($"update-query-string-{renderMode}")).Click();
Browser.Equal("2", () => Browser.Exists(By.Id($"query-{renderMode}")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void SupplyParameterFromQueryGetsUpdatedOnEnhancedNavigation_BothServerAndWebAssembly(string runtimeThatInvokedNavigation)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("LocationChanged/LocationChanging event (server-and-wasm)")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.Id($"update-query-string-{runtimeThatInvokedNavigation}")).Click();
Browser.Equal("1", () => Browser.Exists(By.Id("query-server")).Text);
Browser.Equal("1", () => Browser.Exists(By.Id("query-wasm")).Text);
Browser.Exists(By.Id($"update-query-string-{runtimeThatInvokedNavigation}")).Click();
Browser.Equal("2", () => Browser.Exists(By.Id("query-server")).Text);
Browser.Equal("2", () => Browser.Exists(By.Id("query-wasm")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void LocationChangingEventGetsInvokedOnEnhancedNavigation_OnlyServerOrWebAssembly(string renderMode)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"LocationChanged/LocationChanging event ({renderMode})")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id($"location-changing-count-{renderMode}")).Text);
Browser.Exists(By.Id($"update-query-string-{renderMode}")).Click();
Browser.Equal("1", () => Browser.Exists(By.Id($"location-changing-count-{renderMode}")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void LocationChangingEventGetsInvokedOnEnhancedNavigationOnlyForRuntimeThatInvokedNavigation(string runtimeThatInvokedNavigation)
{
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText("LocationChanged/LocationChanging event (server-and-wasm)")).Click();
Browser.Equal("Page with location changed components", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id("location-changing-count-server")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id("location-changing-count-wasm")).Text);
Browser.Exists(By.Id($"update-query-string-{runtimeThatInvokedNavigation}")).Click();
// LocationChanging event gets invoked only for the interactive runtime that invoked navigation
var anotherRuntime = runtimeThatInvokedNavigation == "server" ? "wasm" : "server";
Browser.Equal("1", () => Browser.Exists(By.Id($"location-changing-count-{runtimeThatInvokedNavigation}")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id($"location-changing-count-{anotherRuntime}")).Text);
}
[Theory]
[InlineData("server")]
[InlineData("wasm")]
public void CanReceiveNullParameterValueOnEnhancedNavigation(string renderMode)
{
// See: https://github.com/dotnet/aspnetcore/issues/52434
Navigate($"{ServerPathBase}/nav");
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Null component parameter ({renderMode})")).Click();
Browser.Equal("Page rendering component with null parameter", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("0", () => Browser.Exists(By.Id("current-count")).Text);
Browser.Exists(By.Id("button-increment")).Click();
Browser.Equal("0", () => Browser.Exists(By.Id("location-changed-count")).Text);
Browser.Equal("1", () => Browser.Exists(By.Id("current-count")).Text);
// This refresh causes the interactive component to receive a 'null' parameter value
Browser.Exists(By.Id("button-refresh")).Click();
Browser.Equal("1", () => Browser.Exists(By.Id("location-changed-count")).Text);
Browser.Equal("1", () => Browser.Exists(By.Id("current-count")).Text);
// Increment the count again to ensure that interactivity still works
Browser.Exists(By.Id("button-increment")).Click();
Browser.Equal("2", () => Browser.Exists(By.Id("current-count")).Text);
// Even if the interactive runtime continues to function (as the WebAssembly runtime might),
// fail the test if any errors were logged to the browser console
var logs = Browser.GetBrowserLogs(LogLevel.Warning);
Assert.DoesNotContain(logs, log => log.Message.Contains("Error"));
}
[Fact]
public void CanUpdateHrefOnLinkTagWithIntegrity()
{
// Represents issue https://github.com/dotnet/aspnetcore/issues/54250
// Previously, if the "integrity" attribute appeared after "href", then we'd be unable
// to update "href" because the new content wouldn't match the existing "integrity".
// This is fixed by ensuring we update "integrity" first in all cases.
Navigate($"{ServerPathBase}/nav/page-with-link-tag/1");
var originalH1Elem = Browser.Exists(By.TagName("h1"));
Browser.Equal("PageWithLinkTag 1", () => originalH1Elem.Text);
Browser.Equal("rgba(255, 0, 0, 1)", () => originalH1Elem.GetCssValue("color"));
Browser.Exists(By.LinkText("Go to page with link tag 2")).Click();
Browser.Equal("PageWithLinkTag 2", () => originalH1Elem.Text);
Browser.Equal("rgba(0, 0, 255, 1)", () => originalH1Elem.GetCssValue("color"));
}
[Theory]
[InlineData(false, false, false)]
[InlineData(false, true, false)]
[InlineData(true, true, false)]
[InlineData(true, false, false)]
// [InlineData(false, false, true)] programmatic navigation doesn't work without enhanced navigation
[InlineData(false, true, true)]
[InlineData(true, true, true)]
// [InlineData(true, false, true)] programmatic navigation doesn't work without enhanced navigation
public void EnhancedNavigationScrollBehavesSameAsBrowserOnNavigation(bool enableStreaming, bool useEnhancedNavigation, bool programmaticNavigation)
{
// This test checks if the navigation to another path moves the scroll to the top of the page,
// or to the beginning of a fragment, regardless of the previous scroll position
string landingPageSuffix = enableStreaming ? "" : "-no-streaming";
string buttonKeyword = programmaticNavigation ? "-programmatic" : "";
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation);
Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}");
// "landing" page: scroll maximally down and go to "next" page - we should land at the top of that page
AssertWeAreOnLandingPage();
var scrollOverride = new ScrollOverrideScope(Browser, useEnhancedNavigation);
try
{
// Staleness check is used to assert enhanced navigation is enabled/disabled, as requested
var elementForStalenessCheckOnNextPage = Browser.Exists(By.TagName("html"));
var button1Id = $"do{buttonKeyword}-navigation";
var button1Pos = Browser.GetElementPositionWithRetry(button1Id);
Browser.SetScrollY(button1Pos);
scrollOverride.ClearLog();
var firstNavigationObservation = BeginEnhancedNavigationObservationIfEnhancedNavigation(
useEnhancedNavigation,
elementForStalenessCheckOnNextPage,
ElementWithTextAppears(By.Id("test-info-2"), "Scroll tests next page"));
Browser.Exists(By.Id(button1Id)).Click();
// "next" page: check if we landed at 0, then navigate to "landing"
AssertWeAreOnNextPage();
WaitStreamingRendersFullPage(enableStreaming);
const string fragmentId = "some-content";
Browser.WaitForElementToBeVisible(By.Id(fragmentId));
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnNextPage);
AssertNoPrematureScrollBeforeDomSwapIfEnhancedNavigation(firstNavigationObservation, "landing -> next navigation");
scrollOverride.AssertNoPrematureScroll("next", "landing -> next navigation");
Assert.Equal(0, Browser.GetScrollY());
var elementForStalenessCheckOnLandingPage = Browser.Exists(By.TagName("html"));
var fragmentScrollPosition = Browser.GetElementPositionWithRetry(fragmentId);
var secondNavigationObservation = BeginEnhancedNavigationObservationIfEnhancedNavigation(
useEnhancedNavigation,
elementForStalenessCheckOnLandingPage,
ElementWithTextAppears(By.Id("test-info-1"), "Scroll tests landing page"));
scrollOverride.ClearLog();
Browser.Exists(By.Id(button1Id)).Click();
// "landing" page: navigate to a fragment on another page - we should land at the beginning of the fragment
AssertWeAreOnLandingPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnLandingPage);
AssertNoPrematureScrollBeforeDomSwapIfEnhancedNavigation(secondNavigationObservation, "next -> landing navigation");
scrollOverride.AssertNoPrematureScroll("landing", "next -> landing navigation");
var button2Id = $"do{buttonKeyword}-navigation-with-fragment";
var thirdNavigationObservation = BeginEnhancedNavigationObservationIfEnhancedNavigation(
useEnhancedNavigation,
elementForStalenessCheckOnNextPage,
ElementWithTextAppears(By.Id("test-info-2"), "Scroll tests next page"));
scrollOverride.ClearLog();
Browser.Exists(By.Id(button2Id)).Click();
AssertWeAreOnNextPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnNextPage);
AssertNoPrematureScrollBeforeDomSwapIfEnhancedNavigation(thirdNavigationObservation, "landing -> next (fragment) navigation");
scrollOverride.AssertNoPrematureScroll("next", "landing -> next (fragment) navigation");
var expectedFragmentScrollPosition = fragmentScrollPosition;
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
}
finally
{
scrollOverride.Dispose();
}
}
[Theory]
[InlineData(false, false, false)]
[InlineData(false, true, false)]
[InlineData(true, true, false)]
[InlineData(true, false, false)]
// [InlineData(false, false, true)] programmatic navigation doesn't work without enhanced navigation
[InlineData(false, true, true)]
[InlineData(true, true, true)]
// [InlineData(true, false, true)] programmatic navigation doesn't work without enhanced navigation
public void EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(bool enableStreaming, bool useEnhancedNavigation, bool programmaticNavigation)
{
// This test checks if the scroll position is preserved after backwards/forwards action
string landingPageSuffix = enableStreaming ? "" : "-no-streaming";
string buttonKeyword = programmaticNavigation ? "-programmatic" : "";
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation);
Navigate($"{ServerPathBase}/nav/scroll-test{landingPageSuffix}");
// "landing" page: scroll to pos1, navigate away
AssertWeAreOnLandingPage();
WaitStreamingRendersFullPage(enableStreaming);
// staleness check is used to assert enhanced navigation is enabled/disabled, as requested
var elementForStalenessCheckOnNextPage = Browser.Exists(By.TagName("html"));
var buttonId = $"do{buttonKeyword}-navigation";
Browser.WaitForElementToBeVisible(By.Id(buttonId));
var landingPagePos1 = Browser.GetElementPositionWithRetry(buttonId) - 100;
Browser.SetScrollY(landingPagePos1);
Browser.Exists(By.Id(buttonId)).Click();
// "next" page: scroll to pos1, navigate away
AssertWeAreOnNextPage();
WaitStreamingRendersFullPage(enableStreaming);
Browser.WaitForElementToBeVisible(By.Id(buttonId));
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnNextPage);
var elementForStalenessCheckOnLandingPage = Browser.Exists(By.TagName("html"));
var nextPagePos1 = Browser.GetElementPositionWithRetry(buttonId) - 100;
// make sure we are expecting different scroll positions on the 1st and the 2nd page
Assert.NotEqual(landingPagePos1, nextPagePos1);
Browser.SetScrollY(nextPagePos1);
Browser.Exists(By.Id(buttonId)).Click();
// "landing" page: scroll to pos2, go backwards
AssertWeAreOnLandingPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnLandingPage);
var landingPagePos2 = 500;
Browser.SetScrollY(landingPagePos2);
Browser.Navigate().Back();
// "next" page: check if we landed on pos1, move the scroll to pos2, go backwards
AssertWeAreOnNextPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnNextPage);
AssertScrollPositionCorrect(useEnhancedNavigation, nextPagePos1);
var nextPagePos2 = 600;
Browser.SetScrollY(nextPagePos2);
Browser.Navigate().Back();
// "landing" page: check if we landed on pos1, move the scroll to pos3, go forwards
AssertWeAreOnLandingPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnLandingPage);
AssertScrollPositionCorrect(useEnhancedNavigation, landingPagePos1);
var landingPagePos3 = 700;
Browser.SetScrollY(landingPagePos3);
Browser.Navigate().Forward();
// "next" page: check if we landed on pos1, go forwards
AssertWeAreOnNextPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnNextPage);
AssertScrollPositionCorrect(useEnhancedNavigation, nextPagePos2);
Browser.Navigate().Forward();
// "scroll" page: check if we landed on pos2
AssertWeAreOnLandingPage();
WaitStreamingRendersFullPage(enableStreaming);
AssertEnhancedNavigation(useEnhancedNavigation, elementForStalenessCheckOnLandingPage);
AssertScrollPositionCorrect(useEnhancedNavigation, landingPagePos2);
}
private void AssertScrollPositionCorrect(bool useEnhancedNavigation, long previousScrollPosition)
{
// from some reason, scroll position sometimes differs by 1 pixel between enhanced and browser's navigation
// browser's navigation is not precisely going backwards/forwards to the previous state
var currentScrollPosition = Browser.GetScrollY();
string messagePart = useEnhancedNavigation ? $"{previousScrollPosition}" : $"{previousScrollPosition} or {previousScrollPosition - 1}";
bool isPreciselyWhereItWasLeft = currentScrollPosition == previousScrollPosition;
bool isPixelLowerThanItWasLeft = currentScrollPosition == (previousScrollPosition - 1);
bool success = useEnhancedNavigation
? isPreciselyWhereItWasLeft
: (isPreciselyWhereItWasLeft || isPixelLowerThanItWasLeft);
Assert.True(success, $"The expected scroll position was {messagePart}, but it was found at {currentScrollPosition}.");
}
private void AssertEnhancedNavigation(bool useEnhancedNavigation, IWebElement elementForStalenessCheck, int retryCount = 3, int delayBetweenRetriesMs = 1000)
{
bool enhancedNavigationDetected = false;
string logging = "";
string isNavigationSuppressed = "";
for (int i = 0; i < retryCount; i++)
{
try
{
enhancedNavigationDetected = !elementForStalenessCheck.IsStale();
Assert.Equal(useEnhancedNavigation, enhancedNavigationDetected);
return;
}
catch (XunitException)
{
var logs = Browser.GetBrowserLogs(LogLevel.Warning);
logging += $"{string.Join(", ", logs.Select(l => l.Message))}\n";
isNavigationSuppressed = (string)((IJavaScriptExecutor)Browser).ExecuteScript("return sessionStorage.getItem('suppress-enhanced-navigation');");
logging += $" isNavigationSuppressed: {isNavigationSuppressed}\n";
// Maybe the check was done too early to change the DOM ref, retry
}
Thread.Sleep(delayBetweenRetriesMs);
}
string expectedNavigation = useEnhancedNavigation ? "enhanced navigation" : "full page load";
string isStale = enhancedNavigationDetected ? "is not stale" : "is stale";
throw new Exception($"Expected to use {expectedNavigation} because 'suppress-enhanced-navigation' is set to {isNavigationSuppressed} but the element from previous path {isStale}. logging={logging}");
}
private void AssertWeAreOnLandingPage()
{
string infoName = "test-info-1";
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 30);
Browser.Equal("Scroll tests landing page", () => Browser.Exists(By.Id(infoName)).Text);
}
private void AssertWeAreOnNextPage()
{
string infoName = "test-info-2";
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 30);
Browser.Equal("Scroll tests next page", () => Browser.Exists(By.Id(infoName)).Text);
}
private void WaitStreamingRendersFullPage(bool enableStreaming)
{
if (enableStreaming)
{
Browser.WaitForElementToBeVisible(By.Id("some-content"));
}
}
private void AssertEnhancedUpdateCountEquals(long count)
=> Browser.Equal(count, () => ((IJavaScriptExecutor)Browser).ExecuteScript("return window.enhancedPageUpdateCount;"));
private ScrollObservation? BeginEnhancedNavigationObservationIfEnhancedNavigation(bool useEnhancedNavigation, IWebElement elementForStalenessCheck, Func<IWebDriver, bool> domMutationPredicate) =>
useEnhancedNavigation ? Browser.BeginScrollObservation(elementForStalenessCheck, domMutationPredicate) : null;
private void AssertNoPrematureScrollBeforeDomSwapIfEnhancedNavigation(ScrollObservation? observation, string navigationDescription)
{
if (observation is not ScrollObservation context)
{
return;
}
ScrollObservationResult result;
try