Skip to content

Commit 357192c

Browse files
committed
Move the id initialization to the enhanced nav supression method.
1 parent 5ef86a1 commit 357192c

File tree

7 files changed

+27
-58
lines changed

7 files changed

+27
-58
lines changed

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,10 @@ public void Navigate(string relativeUrl)
3232
Browser.Navigate(_serverFixture.RootUri, relativeUrl);
3333
}
3434

35-
protected override void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
35+
protected override void InitializeAsyncCore()
3636
{
37-
if (supportEnhancedNavigationSuppression)
38-
{
39-
GrantTestId();
40-
}
41-
4237
// Clear logs - we check these during tests in some cases.
4338
// Make sure each test starts clean.
4439
((IJavaScriptExecutor)Browser).ExecuteScript("console.clear()");
45-
46-
InitializeAsyncCore();
47-
}
48-
49-
protected override void InitializeAsyncCore()
50-
{
51-
}
52-
53-
private void GrantTestId()
54-
{
55-
// We have to be on any page to ensure session storage is available
56-
// Since PageLoadStrategy can be set to None, we need to manually wait for page load
57-
Navigate(ServerPathBase);
58-
59-
// Wait for the page to be completely loaded (equivalent to PageLoadStrategy.Normal)
60-
var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Browser, TimeSpan.FromSeconds(10));
61-
wait.Until(driver =>
62-
{
63-
try
64-
{
65-
// Check if document is ready and load event has fired
66-
var loadComplete = ((IJavaScriptExecutor)driver).ExecuteScript(
67-
"return document.readyState === 'complete' && performance.timing.loadEventEnd > 0");
68-
return (bool)loadComplete;
69-
}
70-
catch
71-
{
72-
return false;
73-
}
74-
});
75-
76-
var testId = Guid.NewGuid().ToString("N")[..8];
77-
((IJavaScriptExecutor)Browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
7840
}
7941
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public EnhancedNavigationTest(
3131
// One of the tests here makes use of the streaming rendering page, which uses global state
3232
// so we can't run at the same time as other such tests
3333
public override Task InitializeAsync()
34-
=> InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
34+
=> InitializeAsync(BrowserFixture.StreamingContext);
3535

3636
[Fact]
3737
public void CanNavigateToAnotherPageWhilePreservingCommonDOMElements()

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
1818
{
1919
var browser = fixture.Browser;
2020

21-
// Set the suppression flag - this will prevent enhanced navigation from being attached on the next navigation
22-
// and trigger detachment of currently attached enhanced navigation via the periodic check
23-
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
24-
if (testId == null)
25-
{
26-
throw new InvalidOperationException("Test ID not found in sessionStorage. Ensure that suppression is enabled for test class by passing `supportEnhancedNavigationSuppression: true` to InitializeAsync or for a given test by calling `GrantTestId()` in the beginning of the test.");
27-
}
28-
2921
if (!skipNavigation)
3022
{
3123
// Normally we need to navigate here first otherwise the browser isn't on the correct origin to access
@@ -34,13 +26,33 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
3426
browser.Equal("Hello", () => browser.Exists(By.TagName("h1")).Text);
3527
}
3628

29+
try
30+
{
31+
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
32+
}
33+
catch (Exception ex)
34+
{
35+
throw new InvalidOperationException("Session storage not found. Ensure that the browser is on the correct origin by navigating to a page or by setting skipNavigation to false.", ex);
36+
}
37+
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
38+
if (testId == null)
39+
{
40+
testId = GrantTestId(browser);
41+
}
42+
3743
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('suppress-enhanced-navigation-{testId}', 'true')");
3844

3945
var suppressEnhancedNavigation = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('suppress-enhanced-navigation-{testId}');");
4046
Assert.True(suppressEnhancedNavigation is not null && (string)suppressEnhancedNavigation == "true",
4147
"Expected 'suppress-enhanced-navigation' to be set in sessionStorage.");
4248
}
4349
}
50+
private static string GrantTestId(IWebDriver browser)
51+
{
52+
var testId = Guid.NewGuid().ToString("N")[..8];
53+
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
54+
return testId;
55+
}
4456

4557
public static long GetScrollY(this IWebDriver browser)
4658
=> Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript("return window.scrollY"), CultureInfo.CurrentCulture);

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override Task InitializeAsync()
3232
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
3333
Directory.CreateDirectory(_tempDirectory);
3434

35-
return InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
35+
return InitializeAsync(BrowserFixture.StreamingContext);
3636
}
3737

3838
[Theory]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public StreamingRenderingTest(
2727
}
2828

2929
public override Task InitializeAsync()
30-
=> InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
30+
=> InitializeAsync(BrowserFixture.StreamingContext);
3131

3232
[Fact]
3333
public async Task CanRenderNonstreamingPageWithoutInjectingStreamingMarkersOrHeaders()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public StatePersistenceTest(
2929

3030
// Separate contexts to ensure that caches and other state don't interfere across tests.
3131
public override Task InitializeAsync()
32-
=> InitializeAsync(BrowserFixture.StreamingContext + _nextStreamingIdContext++, supportEnhancedNavigationSuppression: true);
32+
=> InitializeAsync(BrowserFixture.StreamingContext + _nextStreamingIdContext++);
3333

3434
// Validates that we can use persisted state across server, webassembly, and auto modes, with and without
3535
// streaming rendering.

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,11 @@ public virtual Task InitializeAsync()
5858
return InitializeAsync("");
5959
}
6060

61-
public virtual Task InitializeAsync(string isolationContext, bool supportEnhancedNavigationSuppression = false)
61+
public virtual Task InitializeAsync(string isolationContext)
6262
{
6363
InitializeBrowser(isolationContext);
64-
InitializeAsyncCore(supportEnhancedNavigationSuppression);
65-
return Task.CompletedTask;
66-
}
67-
68-
protected virtual void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
69-
{
7064
InitializeAsyncCore();
65+
return Task.CompletedTask;
7166
}
7267

7368
protected virtual void InitializeAsyncCore()

0 commit comments

Comments
 (0)