0
votes

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' => '[email protected]',
            '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

1
have you tried $browser->loginAs(Admin::find(1)) ->clickLink('New Admin') ->assertSee('Create');pseudoanime
@pseudoanime I got this error. Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"body New Admin"} (Session info: headless chrome=61.0.3163.100) (Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.11.6 x86_64)daniel8x

1 Answers

1
votes

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' => '[email protected]',
        '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.