phplaravellaravel-5laravel-duskbrowser-testing

Having trouble with using laravel dusk


I am a newbie Laravel Dusk. I am trying to write a scenario that click a link a[href] then assertSee something. i have been spending whole day but just cant.

<li>
                    <a href="{{url('/administrator')}}/create" id="create-new-agent" ><div class="pull-left"><i class="zmdi zmdi-smartphone-setup mr-20"></i><span class="right-nav-text">New Admin</span></div><div class="clearfix"></div></a>
                </li>

My scenario

/** @test */
    public function can_create_admin_with_authentication()
    {
        $admin = factory(Admin::class)->create([
            'email' => 'abc@abc.com',
            'password' => bcrypt('123456')
        ]);
        $this->browse(function (MyBrowser $browser) {
            $browser->loginAs(Admin::find(1))
                ->click('a[href="/administrator/create"]')
                ->assertSee('Create');
        });

    }

I am not a big fan of using CSSselector. Is there anyway that i can use xpath or using ID of the link...

Thanks much


Solution

  • I've verified it, you can without any problem instead of:

    ->click('a[href="/administrator/create"]')
    

    use

    ->click('#create-new-agent')
    

    and it will work.

    I see that you also miss running visit() method. The whole test should look like this:

    public function can_create_admin_with_authentication()
    {
        $admin = factory(Admin::class)->create([
            'email' => 'abc@abc.com',
            'password' => bcrypt('123456')
        ]);
        $this->browse(function (MyBrowser $browser) {
            $browser->loginAs(Admin::find(1))->visit('/your/url')
                ->click('#create-new-agent')
                ->assertSee('Create');
        });
    
    }
    

    In place of /your/url put the url you want to visit, for example use / for main page.