使用laravel黄昏时遇到麻烦

时间:2017-10-08 20:21:18

标签: php laravel laravel-5 laravel-dusk browser-testing

我是新手Laravel Dusk。我正在尝试编写一个方案,单击链接[href]然后断言查看某些内容。我一直在度过一整天,但只是不能。

<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>

我的情景

/** @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');
        });

    }

我不是使用CSSselector的忠实粉丝。无论如何我可以使用xpath或使用链接ID ...

非常感谢

1 个答案:

答案 0 :(得分:1)

我已经验证过,您可以毫无问题地代替:

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

使用

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

它会起作用。

我发现你也错过了运行visit()方法。整个测试应如下所示:

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');
    });

}

代替/your/url放置您要访问的网址,例如使用/作为主页。