klreeher
klreeher

Reputation: 1615

Playwright & Accessing sessionStorage

I have an application that I want to test with some automated UI tests, where the devs have stored some of the user's preferences in the sessionStorage. There appears to be a bug where the session storage isn't getting cleared out when we expect it to be. This is the bug I'm trying to recreate.

After fighting with pure Selenium for a while, I tried Playwright, and I almost have this working, but not quite.

I'm using C# (.NET 6) with Playwright.Nunit 1.27.1.

In my test, the user logs into the site, and after the site is logged into, I want to get the preferences object from the session storage. This is the code I'm using for that section:

        // NOW GET SESSION

        // sessionStorage.getItem('settingsObj')
        var sessionStorage = await Page.EvaluateAsync<string>("() => JSON.stringify( sessionStorage.getItem('settingsObj'))");
              
        Console.WriteLine($"STORAGE? {sessionStorage}");
    }

When I run this test, the sessionStorage prints out as a NULL.

However, if I run this test in the debugger, with breakpoints on the var sessionStorage... line and the Console.WriteLine... lines, I see that the sessionStorage DOES contain my object during the run.

Screenshot of Visual Studio 2022 debugger paused mid-run. The cursor is hovering over the sessionStorage variable, and the debugger shows that the value is a non-null JSON string object.

So. How do I get this object OUT so I can parse it?

Upvotes: 2

Views: 4677

Answers (0)

Related Questions