Home > Backend Development > PHP Tutorial > Detailed introduction to Laravel Dusk browser testing is more elegant

Detailed introduction to Laravel Dusk browser testing is more elegant

黄舟
Release: 2023-03-06 19:38:02
Original
1985 people have browsed it

When you start your application using Laravel 5.4, Laravel Dusk brings us an API for testing in the browser. It gives us a built-in ChromeDriver. Of course, if other browsers want to use it, you can use Selenium . 】When your environment supports Laravel 5.4, the first step is to install a demo. We use composer to install Laravel

composer create-project --prefer-dist laravel/laravel demo
Copy after login

Install Laravel Dusk

composer require laravel/dusk
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

in There are two methods to register DuskServiceProvider in your Laravel application

Method 1

We can do it in the config/app.php fileprovidersRegister in array,

App\Providers\RouteServiceProvider::class,Laravel\Dusk\DuskServiceProvider::class,
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

This method will register globally in Laravel. If you don’t want to register globally, we use method 2 .

Method 2

Register AppServiceProvider in the installation environmentDuskServiceProvider

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Dusk\DuskServiceProvider;
class AppServiceProvider extends ServiceProvider{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if ($this->app->environment('local', 'testing', 'staging')) {           
        $this->app->register(DuskServiceProvider::class);
        }
    }
}
Copy after login

Next we installDUSK

#
php artisan dusk:install
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

Let’s start our test

First test

First we complete Laravel’s authentication mechanism.

php artisan make:auth
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

We create a Dusk test

php artisan dusk:make LoginTest
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

The above command will be in## Create a LoginTest class

<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class LoginTest extends DuskTestCase{
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->browse(function ($browser) {
            $browser->visit(&#39;/&#39;)
                    ->assertSee(&#39;Laravel&#39;);
        });
    }
Copy after login
Detailed introduction to Laravel Dusk browser testing is more elegant in the #tests\Browser

directory. Note: A user is required to log in. We have added a test user.

  • Add test user

    1. Execute the command to create the

    User table

    php artisan migrate
    Copy after login

    2.Use

    tinker Command to add 10 pieces of test data

    php artisan tinker
    factory(App\User::class, 10)->create();
    Copy after login

    Of course we can also register ourselves. For testing, you need to know the username and password.

    Email: moocfans@moocfans.cn

    Password: moocfans

    Detailed introduction to Laravel Dusk browser testing is more elegant

    We add a verification in the

    LoginTest classUser login Test case for success and returning the welcome page.

        /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_I_can_login_successfully()
    {
        $this->browse(function ($browser) {
            $browser->visit(&#39;/login&#39;)
                    ->type(&#39;email&#39;, &#39;moocfans@moocfans.cn&#39;)
                    ->type(&#39;password&#39;, &#39;moocfans&#39;)
                    ->press(&#39;Login&#39;)
                    ->assertSee(&#39;You are logged in!&#39;);
        });
    }
    Copy after login

    Next we start testing

    php artisan dusk
    Copy after login

    If your database has correct data, the following results will be returned.

Detailed introduction to Laravel Dusk browser testing is more elegant

Note that the chrome version needs to be >54

Failed test

We can deliberately modify an error The test case,

PHPUnit throws an error message to us. Let’s first change the login password to 1

public function test_I_can_login_successfully()
    {
        $this->browse(function ($browser) {
            $browser->visit(&#39;/login&#39;)
                    ->type(&#39;email&#39;, &#39;moocfans@moocfans.cn&#39;)
                    ->type(&#39;password&#39;, &#39;1&#39;)
                    ->press(&#39;Login&#39;)
                    ->assertSee(&#39;You are logged in!&#39;);
        });
    }
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

The username and password do not match. So there is an error message.

Dusk will put error screenshots directly into \tests\Browser\screenshots so that everyone can find errors more accurately.

Detailed introduction to Laravel Dusk browser testing is more elegant

Testing Ajax calls

#Dusk Of course, you can also test ajax calls.

There is a perfect test case, ajax test demo on github
We can download it and use it directly.

The process of creating a new test case, create a test case

php artisan dusk:make CreateTaskTest

Detailed introduction to Laravel Dusk browser testing is more elegant

Then edit the test case

  class CreateTaskTest extends DuskTestCase{    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_I_can_create_task_successfully()
    {
        $this->browse(function ($browser) {

            $browser->visit(&#39;/tasks/create&#39;)
                    ->type(&#39;title&#39;, &#39;My Task&#39;)
                    ->press(&#39;Add Task&#39;)
                    ->pause(5000)
                    ->assertPathIs(&#39;/tasks&#39;);
        });
    }
}
Copy after login

  • Test case execution process

    1. Enter the title

    2. Click the "Add Task" button
    3. Wait for 5 seconds
    4. Then redirect to the task page

Detailed introduction to Laravel Dusk browser testing is more elegant

We can also use

waitUntilMissing to execute Dusk API

<?phpnamespace Tests\Browser;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class CreateTaskTest extends DuskTestCase{
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_I_can_create_task_successfully()
    {
        $this->browse(function ($browser) {

            $browser->visit(&#39;/tasks/create&#39;)
                    ->type(&#39;title&#39;, &#39;My Task&#39;)
                    ->press(&#39;Add Task&#39;)
                    ->waitUntilMissing(&#39;.btn-primary&#39;)
                    ->assertPathIs(&#39;/tasks&#39;);
        });
    }
}
Copy after login

For more APIs, please check out the Laravel 5.4 documentation

Looking at another example

#Modal dialog box binds your login EMail

Create this test Use case process.

登录
找到 链接 Support Email
单击 Support Email
看到你绑定的EMail
根据上面的过程,我们创建测试用例
首先,先创建一个名为 SupportEmailsTest 测试用例

php artisan dusk:make SupportEmailsTest
Copy after login

编辑测试用例

class SupportEmailsTest extends DuskTestCase{
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_I_can_open_modal_for_support_emails()
    {
        $this->browse(function ($browser) {

            $user = factory(User::class)->create();

            $browser->loginAs($user)
                    ->visit(&#39;/tasks&#39;)
                    ->clickLink(&#39;Support Email&#39;)
                    ->whenAvailable(&#39;#modal-support&#39;, function ($modal) use($user) {
                        $modal->assertInputValue(&#39;#support-from&#39;, $user->email);
                    });
        });
    }
}
Copy after login

我们来执行这个测试用例

php artisan dusk tests/Browser/SupportEmailsTest.php
Copy after login

Detailed introduction to Laravel Dusk browser testing is more elegant

页面

DuskPages 是功能强大的可重用的测试类。
让我们使用 createtasktest 创建页面重构。

php artisan dusk:page CreateTaskPage
Copy after login

创建的页面会存放在 tests/Browser/Pages 目录中

我们来编辑这个类

<p   style="max-width:90%">public function url(){<br/>    return &#39;/tasks/create&#39;;<br/>}<br/></p>
Copy after login

url 可以导航 Dusk 执行的地址。

public function assert(Browser $browser){
    $browser->assertPathIs($this->url());
}
Copy after login

assert 定义页面的 assertions,当使用 CreateTaskPage 时,这些 assertions 将会使用 assert 方法执行。
在上面的例子中,我们只是明确 Url 是正确的。

public function elements(){
    return [        &#39;@addTask&#39; => &#39;.btn-primary&#39;,
    ];
}
Copy after login

elements 方法可以定义选择器。我们可以定义程序可读的名称选择器和重用他们的网页在不同的测试案例。在上面的示例中,我定义了添加任务按钮的选择器。
现在让我们修改 createtasktest 类并使用选择器:

class CreateTaskTest extends DuskTestCase{    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function test_I_can_create_task_successfully()
    {
        $this->browse(function ($browser) {

            $user = factory(User::class)->create();

            $browser->loginAs($user)
                    ->visit(new CreateTaskPage)
                    ->type(&#39;title&#39;, &#39;My Task&#39;)
                    ->click(&#39;@addTask&#39;)
                    ->waitUntilMissing(&#39;@addTask&#39;)
                    ->assertPathIs(&#39;/tasks&#39;);
        });
    }
}
Copy after login

我们修改看了 createtaskpage。现在让我们重新运行我们的测试,看看是否一切正常:
和上面测试一样,因此图我就用了同一个。
Detailed introduction to Laravel Dusk browser testing is more elegant

The above is the detailed content of Detailed introduction to Laravel Dusk browser testing is more elegant. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template