php-laravelHow can I use Laravel Dusk to test my PHP application?
Laravel Dusk is a powerful browser automation tool that can be used to test a PHP application. It allows you to write tests that interact with your application like a real user.
To use Laravel Dusk, you need to install the Dusk package via Composer.
composer require --dev laravel/dusk
Once the package is installed, you need to create a DuskTestCase
class. This class will be used to define and run your Dusk tests.
<?php
namespace Tests;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
static::startChromeDriver();
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--window-size=1920,1080',
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
}
Once the DuskTestCase
is created, you can create test classes that extend it. Each test class will contain the tests you want to run.
<?php
namespace Tests;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* @return void
*/
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
}
Once the tests are written, you can run them using the Dusk command:
php artisan dusk
This will run your tests and provide a report of the results.
Helpful links
More of Php Laravel
- How can I use the @yield directive in PHP Laravel?
- How can I get the current year in PHP Laravel?
- How can I use PHP and Laravel together?
- How can I use React with PHP Laravel?
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How can I convert JSON data to XML using PHP Laravel?
- How can I use PHP and XML to create a Laravel application?
- How do I set up a websocket connection using Laravel and PHP?
- How do I set up a Laravel worker using PHP?
- How do I use Laravel Valet with PHP?
See more codes...