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

Code before:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using OpenQA.Selenium;

namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;

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

            if (!skipNavigation)
            {
                // Normally we need to navigate here first otherwise the browser isn't on the correct origin to access
                // localStorage. But some tests are already in the right place and need to avoid extra navigation.
                fixture.Navigate($"{fixture.ServerPathBase}/");
                browser.Equal("Hello", () => browser.Exists(By.TagName("h1")).Text);
            }

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

    public static long GetScrollY(this IWebDriver browser)
        => Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript("return window.scrollY"), CultureInfo.CurrentCulture);

    public static long SetScrollY(this IWebDriver browser, long value)
        => Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript($"window.scrollTo(0, {value})"), CultureInfo.CurrentCulture);
}


Review comment (suggestion):
Rather than doing this. Can we check `Browser.SessionId == null` and just return? Another option might be `Browser.WindowHandles`

Code after:
            {
                // 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);
            }
            ((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.removeItem('suppress-enhanced-navigation')");
        }
        catch (WebDriverException ex) when (ex.Message.Contains("invalid session id"))
        {
            // Browser session is no longer valid (e.g., browser was closed)
            // Session storage is automatically cleared when browser closes, so cleanup is already done
            // This is expected in some tests, so we silently return
            return;
        }
        finally
        {
            _isSuppressed = false;
        }
    }

    private static void NavigateToOrigin<TServerFixture>(ServerTestBase<TServerFixture> fixture)
        where TServerFixture : ServerFixture
    {
        // Navigate to the test origin to ensure the browser is on the correct state to access sessionStorage
        fixture.Navigate($"{fixture.ServerPathBase}/");
        fixture.Browser.Exists(By.Id("session-storage-anchor"));
    }

    public static long GetScrollY(this IWebDriver browser)
        => Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript("return window.scrollY"), CultureInfo.CurrentCulture);

    public static long SetScrollY(this IWebDriver browser, long value)
        => Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript($"window.scrollTo(0, {value})"), CultureInfo.CurrentCulture);
}