Skip to content

Commit a6856fc

Browse files
committed
Cleanup - removal of storage items can be done once, on disposal.
1 parent 795b665 commit a6856fc

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public void Navigate(string relativeUrl)
3434

3535
public override async Task DisposeAsync()
3636
{
37-
TryCleanSessionStorage();
38-
37+
EnhancedNavigationTestUtil.CleanEnhancedNavigationSuppression(this);
3938
await base.DisposeAsync();
4039
}
4140

@@ -45,17 +44,4 @@ protected override void InitializeAsyncCore()
4544
// Make sure each test starts clean.
4645
((IJavaScriptExecutor)Browser).ExecuteScript("console.clear()");
4746
}
48-
49-
private void TryCleanSessionStorage()
50-
{
51-
try
52-
{
53-
EnhancedNavigationTestUtil.CleanEnhancedNavigationSuppression(this);
54-
}
55-
catch (Exception ex)
56-
{
57-
// exception here most probably means session storage is not available - no cleanup needed
58-
Output?.WriteLine("Error when removing test id from session storage: " + ex.Message);
59-
}
60-
}
6147
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void RefreshCanFallBackOnFullPageReload(string renderMode)
318318
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
319319

320320
// if we don't clean up the suppression, all subsequent navigations will be suppressed by default
321-
EnhancedNavigationTestUtil.CleanEnhancedNavigationSuppression(this);
321+
EnhancedNavigationTestUtil.CleanEnhancedNavigationSuppression(this, skipNavigation: true);
322322

323323
// Normally, you shouldn't store references to elements because they could become stale references
324324
// after the page re-renders. However, we want to explicitly test that the element becomes stale

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

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,24 @@ public static class EnhancedNavigationTestUtil
1414
public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool shouldSuppress, bool skipNavigation = false)
1515
where TServerFixture : ServerFixture
1616
{
17-
var browser = fixture.Browser;
18-
19-
if (!skipNavigation)
17+
if (shouldSuppress)
2018
{
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-
}
19+
var browser = fixture.Browser;
2520

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-
}
21+
if (!skipNavigation)
22+
{
23+
NavigateToOrigin(fixture);
24+
}
3425

35-
// This prevents test interference where suppression state from previous tests persists
36-
CleanEnhancedNavigationSuppression(fixture);
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+
}
3734

38-
if (shouldSuppress)
39-
{
4035
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
4136
if (testId is null || string.IsNullOrEmpty(testId as string))
4237
{
@@ -51,12 +46,26 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
5146
}
5247
}
5348

54-
public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTestBase<TServerFixture> fixture)
49+
public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool skipNavigation = false)
5550
where TServerFixture : ServerFixture
5651
{
5752
var browser = fixture.Browser;
5853

59-
// only tests that suppress enhanced navigation will have these items set, so it can throw
54+
// First, ensure we're on the correct origin to access sessionStorage
55+
try
56+
{
57+
// Check if we can access sessionStorage from current location
58+
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
59+
}
60+
catch
61+
{
62+
if (skipNavigation)
63+
{
64+
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.");
65+
}
66+
NavigateToOrigin(fixture);
67+
}
68+
6069
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
6170
if (testId is null || string.IsNullOrEmpty(testId as string))
6271
{
@@ -67,6 +76,14 @@ public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTest
6776
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('suppress-enhanced-navigation-{testId}')");
6877
}
6978

79+
private static void NavigateToOrigin<TServerFixture>(ServerTestBase<TServerFixture> fixture)
80+
where TServerFixture : ServerFixture
81+
{
82+
// Navigate to the test origin to ensure the browser is on the correct state to access sessionStorage
83+
fixture.Navigate($"{fixture.ServerPathBase}/");
84+
fixture.Browser.Equal("Hello", () => fixture.Browser.Exists(By.TagName("h1")).Text);
85+
}
86+
7087
private static string GrantTestId(IWebDriver browser)
7188
{
7289
var testId = Guid.NewGuid().ToString("N")[..8];

0 commit comments

Comments
 (0)