Dusk
Mixing up Browser and API[Feature/Unit] test in Laravel.

Mixing up Browser and API[Feature/Unit] test in Laravel.

In my case, I wanted to test first a request which only was able to execute using api from mobile devices. And once the api is requested, I needed to test datas in browser. So, for me I needed mix of browser and feature testing. I found the recommended tool for browser testing is Laravel Dusk. But I was not able to mix them both. Are you also searching for the same? Well, then this blog is for you. If not, no problem, let me share you what I have learnt 👽.

Well, first and foremost, you will need to install dusk anyway because we are going to use dusk to integrate both feature and browser testing. Follow these steps to install Dusk.

composer require --dev laravel/dusk
php artisan dusk:install
php artisan dusk:make OrderUpdateTest

Once you enter all 3 commands, you will be ready to use dusk. Now, you can try with browser testing, it will work. Write these codes in your dusk test class OrderUpdateTest which we created using php artisan command above.

$user = User::find(1);
$this->actingAs($user, 'api');

$edit_order_request = [
    "order_id" => 101,
    "status" => SMState::c_completed,
];

$response = $this->actingAs($user)->json(
    'POST',
    route('api.updateOrder') ?? '',
);

Now update DuskTestCase which you will find as tests/DuskTestCase.php. This will help you call migrate fresh and seed once when dusk or test is called. It will be important if you are trying to fix api and browser testing. However, if you are trying to do only browser testing and it might not be important to call migration and seeding. In my case, I required migration. So, the way I find out to execute migration was like this.

protected static bool $migrationRun = false;

public function setUp(): void{
    parent::setUp();

    if(!static::$migrationRun){
        $this->artisan('migrate:fresh --seed');
        static::$migrationRun = true;
    }
}

Please note, don’t use a trait DatabaseMigrations, if you use this, you wont be able to run the test. You will get error as follow.

actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

Once you go setUp() approach, you will be able to call TestCase call properties and methods like actingAs() and such others.

Nex thing, you will to take care is about .env file. If you are using dusk, it is better to create new file with a name .env.dusk.local. It is always good to use .local at the end. Trust me 🙂. Once you create that, feel information as per your need for database and other stuffs. And now you are good to go using dusk as separate database. If you don’t use separate database, remember all of your data will be cleared from your local database as we have used migrate:fresh --seed command.

php artisan dusk

Run this code to check if dusk is working properly or not. So, the idea is, instead of creating TestCase based feature test, I created Dusk test class, and I included my feature testing and browser testing in dusk class. Also, you don’t need to call php artisan dusk and php artisan test to run all test. You can just call php artisan test. This will call dusk testes as well.

Let me know if you need any help or if you are confused with my steps to implement testes.

Thanks

Leave a Reply