@@ -22,20 +22,12 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
2222 {
2323 NavigateToOrigin ( fixture ) ;
2424 }
25-
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- }
25+ AssertSessionStorageAvailable ( browser ) ;
3426
3527 var testId = ( ( IJavaScriptExecutor ) browser ) . ExecuteScript ( $ "return sessionStorage.getItem('test-id')") ;
3628 if ( testId is null || string . IsNullOrEmpty ( testId as string ) )
3729 {
38- testId = GrantTestId ( browser ) ;
30+ throw new InvalidOperationException ( "Test ID not found in sessionStorage. Make sure your test class grants it in InitializeAsync." ) ;
3931 }
4032
4133 ( ( IJavaScriptExecutor ) browser ) . ExecuteScript ( $ "sessionStorage.setItem('suppress-enhanced-navigation-{ testId } ', 'true')") ;
@@ -46,6 +38,27 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
4638 }
4739 }
4840
41+ public static void AssertSessionStorageAvailable ( IWebDriver browser )
42+ {
43+ try
44+ {
45+ ( ( IJavaScriptExecutor ) browser ) . ExecuteScript ( "sessionStorage.length" ) ;
46+ }
47+ catch ( Exception ex )
48+ {
49+ 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 ) ;
50+ }
51+
52+ }
53+
54+ public static void GrantTestId < TServerFixture > ( ServerTestBase < TServerFixture > fixture )
55+ where TServerFixture : ServerFixture
56+ {
57+ NavigateToOrigin ( fixture ) ;
58+ AssertSessionStorageAvailable ( fixture . Browser ) ;
59+ GrantTestIdCore ( fixture . Browser ) ;
60+ }
61+
4962 public static void CleanEnhancedNavigationSuppression < TServerFixture > ( ServerTestBase < TServerFixture > fixture , bool skipNavigation = false )
5063 where TServerFixture : ServerFixture
5164 {
@@ -94,7 +107,7 @@ private static void NavigateToOrigin<TServerFixture>(ServerTestBase<TServerFixtu
94107 fixture . Browser . Equal ( "Hello" , ( ) => fixture . Browser . Exists ( By . TagName ( "h1" ) ) . Text ) ;
95108 }
96109
97- private static string GrantTestId ( IWebDriver browser )
110+ private static string GrantTestIdCore ( IWebDriver browser )
98111 {
99112 var testId = Guid . NewGuid ( ) . ToString ( "N" ) [ ..8 ] ;
100113 ( ( IJavaScriptExecutor ) browser ) . ExecuteScript ( $ "sessionStorage.setItem('test-id', '{ testId } ')") ;
0 commit comments