Skip to content

Commit 5696430

Browse files
committed
Tests that closed the browser do not have to clean the session storage.
1 parent 763ea9d commit 5696430

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,39 @@ public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTest
5151
{
5252
var browser = fixture.Browser;
5353

54-
// First, ensure we're on the correct origin to access sessionStorage
5554
try
5655
{
57-
// Check if we can access sessionStorage from current location
58-
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
59-
}
60-
catch
61-
{
62-
if (skipNavigation)
56+
// First, ensure we're on the correct origin to access sessionStorage
57+
try
58+
{
59+
// Check if we can access sessionStorage from current location
60+
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
61+
}
62+
catch
6363
{
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.");
64+
if (skipNavigation)
65+
{
66+
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.");
67+
}
68+
NavigateToOrigin(fixture);
6569
}
66-
NavigateToOrigin(fixture);
67-
}
6870

69-
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
70-
if (testId is null || string.IsNullOrEmpty(testId as string))
71+
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
72+
if (testId is null || string.IsNullOrEmpty(testId as string))
73+
{
74+
return;
75+
}
76+
77+
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('test-id')");
78+
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('suppress-enhanced-navigation-{testId}')");
79+
}
80+
catch (WebDriverException ex) when (ex.Message.Contains("invalid session id"))
7181
{
82+
// Browser session is no longer valid (e.g., browser was closed)
83+
// Session storage is automatically cleared when browser closes, so cleanup is already done
84+
// This is expected in some tests, so we silently return
7285
return;
7386
}
74-
75-
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('test-id')");
76-
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('suppress-enhanced-navigation-{testId}')");
7787
}
7888

7989
private static void NavigateToOrigin<TServerFixture>(ServerTestBase<TServerFixture> fixture)

0 commit comments

Comments
 (0)