Repository: dotnet/aspnetcore
File: src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs (C#)

Code before:
            var browser = fixture.Browser;

            if (!skipNavigation)
            {
                NavigateToOrigin(fixture);
            }

            try
            {
                ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
            }
            catch (Exception ex)
            {
                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);
            }

            var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
            if (testId is null || string.IsNullOrEmpty(testId as string))
            {
                testId = GrantTestId(browser);
            }

            ((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('suppress-enhanced-navigation-{testId}', 'true')");

            var suppressEnhancedNavigation = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('suppress-enhanced-navigation-{testId}');");
            Assert.True(suppressEnhancedNavigation is not null && (string)suppressEnhancedNavigation == "true",
                "Expected 'suppress-enhanced-navigation' to be set in sessionStorage.");
        }
    }

    public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool skipNavigation = false)
        where TServerFixture : ServerFixture
    {
        var browser = fixture.Browser;

        try
        {
            // First, ensure we're on the correct origin to access sessionStorage
            try
            {
                // Check if we can access sessionStorage from current location
                ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
            }
            catch
            {
                if (skipNavigation)
                {
                    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.");
                }
                NavigateToOrigin(fixture);
            }

Review comment (suggestion):
If instead of grabbing the test id locally and storing it on session storage you create one and return it from this method (maybe you store it in a local variable or a field in the class) then you shouldn't need to set it within session storage.

You can pass the `test-id` to any method that needs to use it to clear things up later. It's good that we are "randomizing" the `suppress-enhanced-navigation-` bit, but we still run the risk of polluting the state if we rely on a well-known test-id key.

The other thing is that if we always use an "ephemeral" id that we only store in a field/local variable then we don't ever need to do cleanup.

Code after:
        if (shouldSuppress)
        {
            var browser = fixture.Browser;

            if (!skipNavigation)
            {
                NavigateToOrigin(fixture);
            }

            try
            {
                ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
            }
            catch (Exception ex)
            {
                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);
            }

            ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.setItem('suppress-enhanced-navigation', 'true')");

            var suppressEnhancedNavigation = ((IJavaScriptExecutor)browser).ExecuteScript("return sessionStorage.getItem('suppress-enhanced-navigation');");
            Assert.True(suppressEnhancedNavigation is not null && (string)suppressEnhancedNavigation == "true",
                "Expected 'suppress-enhanced-navigation' to be set in sessionStorage.");
            _isSuppressed = true;
        }
    }

    public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool skipNavigation = false)
        where TServerFixture : ServerFixture
    {
        if (!_isSuppressed)
        {
            return;
        }

        var browser = fixture.Browser;

        try
        {
            // First, ensure we're on the correct origin to access sessionStorage
            try
            {
                // Check if we can access sessionStorage from current location
                ((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
            }
            catch
            {
                if (skipNavigation)
                {
                    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.");
                }