Application testing is often a challenging part of the application development life cycle but the Laravel Framework did the task more easy with Laravel Dusk official testing package. It provides a simple and clean API to test your application. If you are very beginner in this term don't worry after the end of this article you will be able to write your browser automated test for laravel application easily!
Testing Scenario
Suppose, We have a blog website, where we have to test our login system and frontend pages are working or not. It's a very painful task to test all pages are working or not after any changes. In this scenario, We'll use Laravel Dusk package to automated test our website. Let's start.
Install Laravel Dusk
composer require --dev laravel/dusk
Run dusk:install
command
php artisan dusk:install
Our installation finished. First, delete the ExampleTest
file from tests/Browser
directory. Let's write our first testing with the login test. Run this command to make a login test file in tests/Browser
the directory.
php artisan dusk:make LoginTest
Now open the LoginTest.php
file and put the code to test the login system working correctly.
<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class LoginTest extends DuskTestCase
{
public function testLogin()
{
$this->browse(function ($browser) {
$browser->visit('/login')
->type('email','jhon@example.com')
->type('password', '12345')
->press('Login')
->assertPathIs('/home');
});
}
}
Explanation: After running this test with php artisan dusk
command. It'll automatically go to the login page and log in to the system by your provided email and password. After login, if the path found /home
then it'll show the test success. If the test gets failure then it'll automatically be snapped a screenshot for you into the tests/Browser/screenshots
directory. So that you can find the problem easily.
Let's make another test file to test our frontend pages. Here we will test our some page like home page, blog page, contact page. You can do whatever page of your website.
php artisan dusk:make FrontendTest
Open the FrontendTest.php file and write these code.
<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class FrontendTest extends DuskTestCase
{
public function homePage()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertTitle('Home - Laravel Article');
});
}
public function blogPage()
{
$this->browse(function (Browser $browser) {
$browser->visit('/blog')
->assertSee('Blog Posts');
});
}
public function contactPage()
{
$this->browse(function (Browser $browser) {
$browser->visit('/contact')
->assertSee('Contact');
});
}
}
Explanation: Here I have written 3 methods to test 3 pages.
homePage
method - Here program will automatically visit our homepage and look for title exactly Home - Laravel Article
if not then the test will fail.
blogPage
method - Here program will automatically visit our /blog
page and look for exact Blog Posts
text on-page body if not then the test will fail.
contactPage
method - Here program will automatically visit our /contact
page and look for exact Contact
text on-page body if not then the test will fail.
After writing all of our tests we have to run command to test our all pages.
php artisan dusk
Here I'have used only 2 assert method assertTitle, assertSee
. There is more helpful assert function for accomplishing your automated test. All available dusk assert helper functions list.
Hope you will find this article helpful to learn automated browser testing for your laravel application. If you find it helpful then please share this with others.