I feel like typically you would want a fresh browser instance for each test in your test class so that each test is starting out in a "fresh" state. Basically serving the same purpose as Laravel's DatabaseTransactions/RefreshDatabase testing traits.
However, if you do not want to login every time/every test method, you could try something similar to the following:
class ExampleTest extends DuskTestCase
{
/**
* An single instance of our browser.
*
* @var Browser|null
*/
protected static ?Browser $browser = null;
/**
* Get our test class ready.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
if (is_null(static::$browser)) {
$this->browse(function (Browser $browser) {
$browser->loginAs(1);
static::$browser = $browser;
});
}
}
/** @test */
public function first_logged_in_use_case()
{
static::$browser->visit('/admin/dashboard')->assertABC();
}
/** @test */
public function second_logged_in_use_case()
{
static::$browser->visit('/admin/dashboard')->assertXYZ();
}
}
I haven't tested this but essentially you're creating a static class property and assigning a logged in browser instance to it. Then you can use that same instance across all your test methods in your class.