2

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?

klreeher
  • 1,391
  • 2
  • 15
  • 27
  • Did you find solution? – johannesMatevosyan Dec 12 '22 at 13:53
  • 1
    @johannesMatevosyan alas, I have not. if I do, I will provide an update. – klreeher Jan 08 '23 at 14:24
  • Far from a perfect solution but what I did to workaround this was to loop check session storage until my expected item was not null -- `string? sessionStorage = null; do { sessionStorage = await page.EvaluateAsync("window.sessionStorage.getItem('item')"); } while (string.IsNullOrWhiteSpace(sessionStorage));` – Brian von Kuster Mar 23 '23 at 16:41

0 Answers0