Hey, in Dusk 2.0 the concept of Pages was introduced. This means you can extend the base Page component and add complex functionality into there, so your example above would become:
tests/Browser/Pages/LoginPage.php
<?php
namespace Tests\Browser\Pages;
use App\Models\User;
use Laravel\Dusk\Browser;
class LoginPage extends Page
{
/**
* Get the URL for the page.
*
* @return string
*/
public function url()
{
return '/login';
}
/**
* Abstract the Login functionality
*
* @param \Laravel\Dusk\Browser $browser
* @param string $name
* @return void
*/
public function login(Browser $browser)
{
$browser->loginAs(User::where('email', '[email protected]')->firstOrFail);
}
}
and then in EmployeesTest.php:
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\Browser\Pages\LoginPage;
class EmployeesTest extends DuskTestCase
{
/**
* Verify if the employees list is showing
*
* @return void
*/
public function testEmployeeListIsShowing()
{
$this->browse(function ($browser) {
$browser->on(new LoginPage)
->login()
->visit('/employees/list')
->waitFor('#employees-datalist');
});
}
/**
* Open the Create employee form
*
* @return void
*/
public function testOpenCreateEmployeeForm()
{
$this->browse(function($browser) {
$browser->on(new LoginPage)
->login()
->visit('/employees/list')
->waitFor('#employees-create-btn');
});
}
}