How can I store items in localstorage before running a test with Laravel Dusk?

Viewed 1851

I'm using Laravel 5.4 and I wonder if it is possible to save items in the localStorage before running a test in Laravel Dusk. For example, imagine that you want to test some behaviours that should happen after completing a long list of things that are stored in the browser's localStorage.

Is it possible to have them in the localStorage before running the test that requires them? If so, how would you do it before a test?

Of course I could write a script that goes through the long process of filling the localStorage manually, as a regular user would do it. But running the same long lasting method over and over just to test bits of functionality doesn't feel quite right.

Thanks for your help, beforehand.

1 Answers

Ok. This is how I did it, thanks to @JonasStaudenmeir comments I managed to do it somehow, so all the credit goes to Jonas!!

So, for the other troubled souls that can find this useful... In my case I needed the localStorage to have some initial data. But as mentioned, I was getting errors, either chaining ->script() or not. So basically I had to visit the page twice, to prevent the errors from showing and to reflect the stored data.

    $browser->visit("/demo")
        ->script("window.localStorage.setItem('picture-dictionary', 'bar')");

    $browser->visit("/demo")
        ->assertSee("TRY IT");
Related