Browser Testing
Adapt can create databases for your Dusk browser tests too, letting you take advantage of its functionality - including the ability to run them when running tests in parallel.
WARNING
This functionality is new and experimental.
Simply create your Dusk tests like normal, and make these two changes:
- Replace Laravel's
DatabaseMigrations
trait withAdaptDatabase
, and - Tell the
$browser
object to use Adapt by adding$this->useAdapt($browser);
before making any requests.
⚙️ See configuration
<?php
// tests/Browser/MyDuskTest.php
namespace Tests\Browser;
use App\Models\User;
use CodeDistortion\Adapt\AdaptDatabase;
//use Illuminate\Foundation\Testing\DatabaseMigrations; // not needed
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class MyDuskTest extends DuskTestCase
{
use AdaptDatabase;
// use DatabaseMigrations; // not needed
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$this->useAdapt($browser);
$user = User::factory()->create();
$browser->loginAs($user)->visit('/')->assertSee("Hello $user->name");
});
}
}
When Dusk tests are run, transaction re-use is turned off automatically.
NOTE
When browser testing, please make sure your .env.testing
session-driver isn't set to "array". e.g.
# .env.testing
SESSION_DRIVER=database
TIP
If your project doesn't have a database, you can still use perform browser testing in parallel. Just tell Adapt to not build databases.
Browser Tests as a Test Suite
Running your Dusk tests with php artisan dusk
might work. However, if you do, Laravel goes through the process of copying .env.dusk.local
over .env
, which is now unnecessary. It also takes control of your local environment for the duration of the test-run.
Instead, turn your browser tests into a normal PHPUnit test suite by adding the ./test/Browser
directory to your phpunit.xml
file.
// phpunit.xml
<testsuites>
…
<testsuite name="Browser">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
</testsuites>
Then you'll be able to run your tests like normal. Your browser tests will now be included in the run.
Your config settings (from .env.testing
) are now used, and your .env.dusk.local
file is ignored.