Skip to content

Commit 795b665

Browse files
committed
Fix tests.
1 parent ee4aa34 commit 795b665

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,31 @@ public static class EnhancedNavigationTestUtil
1414
public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool shouldSuppress, bool skipNavigation = false)
1515
where TServerFixture : ServerFixture
1616
{
17-
if (shouldSuppress)
17+
var browser = fixture.Browser;
18+
19+
if (!skipNavigation)
1820
{
19-
var browser = fixture.Browser;
21+
// Navigate here first to ensure the browser is on the correct origin to access sessionStorage
22+
fixture.Navigate($"{fixture.ServerPathBase}/");
23+
browser.Equal("Hello", () => browser.Exists(By.TagName("h1")).Text);
24+
}
2025

21-
if (!skipNavigation)
22-
{
23-
// Normally we need to navigate here first otherwise the browser isn't on the correct origin to access
24-
// sessionStorage. But some tests are already in the right place and need to avoid extra navigation.
25-
fixture.Navigate($"{fixture.ServerPathBase}/");
26-
browser.Equal("Hello", () => browser.Exists(By.TagName("h1")).Text);
27-
}
26+
try
27+
{
28+
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
29+
}
30+
catch (Exception ex)
31+
{
32+
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);
33+
}
2834

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-
}
35+
// This prevents test interference where suppression state from previous tests persists
36+
CleanEnhancedNavigationSuppression(fixture);
37+
38+
if (shouldSuppress)
39+
{
3740
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
38-
if (testId == null)
41+
if (testId is null || string.IsNullOrEmpty(testId as string))
3942
{
4043
testId = GrantTestId(browser);
4144
}
@@ -55,7 +58,7 @@ public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTest
5558

5659
// only tests that suppress enhanced navigation will have these items set, so it can throw
5760
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
58-
if (testId == null)
61+
if (testId is null || string.IsNullOrEmpty(testId as string))
5962
{
6063
return;
6164
}

0 commit comments

Comments
 (0)