stevgouws's avatar

How do I make one Dusk test carry on from another?

Pretty new to testing in general so I might be missing something basic here:

I'm trying to just logically divide parts of my tests to different functions in the same test class.

Say I want to delete a Player, and I after that I want to confirm if I search for them I don't see them.

I call the second test at the end of the first and pass it the browser instance, and $player variable.

$this->testPlayerNotVisibleInIndex($browser, $player);

It seems to work as I can see Dusk opening the browser and doing exactly what I want, including the second test of filtering for the player after deletion, but I get this error in the test results:

ErrorException: Missing argument 1 for Tests\Browser\Players\SoftDeletePlayerTest::testPlayerNotVisibleInIndex()

Am I doing it completely the wrong way? Are you not supposed to let one function carry over to another? Some of the tests I'm doing are much much longer than this one, it would be great to break it up a bit.

Any help or advice in general would be appreciated.

Here is the whole file:

<?php

namespace Tests\Browser\Players;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\User;
use App\Player;

class SoftDeletePlayerTest extends DuskTestCase
{
    use DataBaseMigrations;

    /**
     * Test creating Player
     *
     * @return void
     */

    public function testSoftDeletePlayer()
    {
        $user = factory(User::class)->create([
            'role_id' => 1,
            'language_id' => 2
        ]);

        $player = factory(Player::class)->create();

        $this->browse(function (Browser $browser) use ($user, $player) {
            $browser->loginAs($user->id)
                ->visit('/players/' . $player->id . '/edit')
                ->assertPathIs('/players/' . $player->id . '/edit')
                ->click('#delete_button')
                ->waitFor('.modal-header')
                ->press('Delete')
                ->assertPathIs('/players');
                $this->testPlayerNotVisibleInIndex($browser, $player);
        });
    }

    public function testPlayerNotVisibleInIndex($browser, $player)
    {
            $browser->type('input[type=search]', $player->name)
                ->pause(1500)
                ->assertDontSee($player->name)
                ->assertDontSee($player->email)
                ->assertDontSee($player->cellphone)
                ->assertSee('No data available in table');
    }
}
0 likes
3 replies
njbarrett's avatar

Hey im new to testing myself. You're on the right track but you dont need to tell Dusk to go onto the next test. It will run all the tests in your class automatically. So literally just remove the line

$this->testPlayerNotVisibleInIndex($browser, $player);

should work I think.

stevgouws's avatar

Hey @njbarrett thanks for the reply. The problem is that then you have to create a new instance of the browser. So if I remove that line the next instance can't just carry on with where it left off, in fact it immediately redirects me to the login page because I am no longer authenticated.

If I log in again, nothing I did in the first test is persisted.

Feel like I'm really missing something basic here. Also can't seem to find any examples online of a series of Dusk tests like what I'm trying to do.

stevgouws's avatar

I realise now I could make the data persist between one test and another by NOT using DatabaseMigrations, although then I have to cleanup the db myself after all the tests run, and I still have to use loginAs again for every individual test which seems like needless repetition.

Please or to participate in this conversation.