How to reuse a browser instance Dusk test?

My project is in Laravel 5.4 framework and I use Dusk for browser tests. I have a page with several sections that I would like to check on my own, however, I encountered a problem when I need to launch a new browser instance, log in and go to this page for each individual test.

public function testExample() { $this->browse(function (Browser $browser) { $browser->loginAs(1) ->visit('/admin/dashboard') ->assertABC() ->assertXYZ(); }); } 

So, when I have 4-5 of them in the class allTheThingsTest extends DuskTestCase , I create 4-5 browser instances for each test class. Obviously, this quickly gets out of hand, especially when I run all of my pre-deployments.

One browser instance per test class is acceptable as far as I know, but I cannot figure out how to do this. So here is what I ask:

  • Is it possible to remember / reuse a browser instance between test functions within the same test class?
  • If so, how?
+7
php laravel laravel-dusk
source share
1 answer

Quote from laravel docs:

Sometimes you can bind something into a container that should only be allowed once, and the same instance should be returned on subsequent calls to the container:

 $this->app->singleton('FooBar', function($app) { return new FooBar($app['SomethingElse']); }); 
+1
source share

All Articles