Thursday, 1 February 2024

Fake data & Testing withLaravel Dusk other system

I. Fake Data with Laravel for other system

- Build laravel project

- Config to connect databases of other system, with files

.env, config/database.php

- Using models, factories, migrations, seeders, faker inlaravel to fake data 


II. Using Laravel Dusk to test for other system

1. Install Laravel Dusk

composer require laravel/dusk --dev

php artisan dusk:install


More detail: https://laravel.com/docs/10.x/dusk 

2. Generating & Run Tests

Generating Test

php artisan dusk:make LoginTest


Example: test login success

<?php


namespace Tests\Browser\External;


use App\Models\User;

use Laravel\Dusk\Browser;

use Tests\DuskTestCase;


class ExternalTest extends DuskTestCase

{

    public function test_login_successful(): void

    {

        $this->browse(function (Browser $browser) {

            $browser->visit('http://demo.test/login')

                    ->type('email', 'admin@example.com')

                    ->type('password', 'password')

                    ->click('#login-button')

                    ->assertSee('Login successful');

        });

    }

}



Example: test create object error with error message

<?php


namespace Tests\Browser\External;


use App\Models\User;


use Laravel\Dusk\Browser;

use Tests\DuskTestCase;


class External2Test extends DuskTestCase

{


    public function test_page_create_post_then_return_error_mgs(): void

    {

        $this->browse(function (Browser $browser) {

            $browser->visit('http://demo.test/admin/post/create')

            ->type('title', ‘post’')

            ->type('keyword', 'demo ')

            ->type('content', 'Your text goes here')
            ->attach('input[name="image"]', __DIR__.'/files/your-file.jpg')  

            ->click('#bt-submit-form-post')

            ->pause(1000)

            ->assertSee('Trường tiêu đề bài viết phải lớn hơn 5 ký tự.');

        });

    }

}



Run Tests

php artisan dusk


3. Methods supoort when test with laravel dusk

* Support interact with browser
- InteractsWithAuthentication: loginAs, logout, currentUserInfo, assertAuthenticated...
- InteractsWithCookies: cookie, plainCookie, addCookie, deleteCookie…
- InteractsWithElements: elements, clickLink, type, typeSlowly, append, clear, select, radio, check, uncheck, attach, press, pressAndWaitFor, drag, acceptDialog, typeInDialog, dismissDialog...
- InteractsWithJavascript: script
- InteractsWithKeyboard: withKeyboard,parseKeys
- InteractsWithMouse: moveMouse, mouseover, click, clickAtXPath, clickAndHold, doubleClick, rightClick…
- WaitsForElements: whenAvailable, waitFor, waitUntilMissing, waitUntilMissingText, waitForText, waitForTextIn, waitForLink, waitForInput, waitForRoute, waitUntilEnabled, waitForDialog, waitForReload, waitForEvent….
* Support assert results
- MakesAssertions: assertTitle, assertHasCookie, assertSee, assertDontSee, assertSeeIn, assertSourceHas… (too many)
- MakesUrlAssertions: assertUrlIs, assertHostIs, assertPortIs, assertPathIs, assertPathIsNot, assertRouteIs
Thank you

References: https://laravel.com/docs/10.x/dusk

No comments:

Post a Comment

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...