- Getting Started
- CI/CD
- Roadmap
- Integrations
Laravel
Puth has a native package for Laravel. It contains a drop-in replacement for Laravel Dusk. This package only provides a Puth client with Laravel bindings, but you also need a Puth Server.
No, you don't need to install Laravel Dusk (or chromedriver or chrome). If you already have tests based on Laravel Dusk then you can simply install the puth/laravel package along the laravel/dusk package and convert your tests piece by piece or write new tests with Puth (both packages won't collide).
Installation
The puth/laravel
package provides the drop-in replacement for Laravel Dusk. Make sure that you have at least Laravel version 9 or higher installed.
$ composer require --dev puth/laravel:0.7.1
After installing, run the puth:install
Artisan command. This will publish the package config and create a tests/PuthTestCase.php
file.
$ php artisan puth:install
Next, configure where your Puth Server is running in config/puth.php
.
Running tests
Add thetests/Browser
directory to your phpunit.xml
so when running php artisan test
your Puth tests will get executed alongside the other tests (don't use the Laravel Dusk command for Puth tests). If you already have existing Dusk tests inside the tests/Browser
directory, i recommend you create a new directory (e.g. tests/Puth
) for Puth tests and add that one to phpunit.xml
instead. You can now run your tests.
$ php artisan test
Usage
Since the Puth Laravel package provides a drop-in dusk replacement, you can start with the example test below. After that you can go to the Laravel Dusk documentation and use Puth like it is Laravel Dusk. The following sections of the documentation fully apply (except of the artisan commands):
- Browser Basics
- Interacting With Elements
- Available Assertions
- Pages
- Components
Example Test
<?php
use Puth\Laravel\Browser\Browser;
use Tests\PuthTestCase;
class ExampleBrowserTest extends PuthTestCase
{
function test_visit_website()
{
$this->browse(function (Browser $browser) {
$browser->visit('https://playground.puth.dev')
->assertSee('Puth');
});
}
}
Differences to Laravel Dusk
Changed functions
$browser->keys()
This method no longer uses the php-webdriver keymap. Instead the puppeteer keymap is used and it is case sensitive!$browser->typeInDialog(selector, value)
Please use the accept method which now takes a value $browser->acceptDialog(value) (more information)
Unsupported functions
$browser->maximize()
Puppeteer has no way of controlling the actual browser window$browser->move($x = 100, $y = 100)
Puppeteer has no way of controlling the actual browser window$browser->moveMouse($xOffset, $yOffset)
Puppeteer doesn't have an actual mouse therefore can't move it by an offset. We could track the mouse x and y location but then we need to update it on $page->click, $element->click, ...$browser->ensurejQueryIsAvailable()
Puppeteer doesn't come with jquery because it's not needed
Dialogs
If you want to use the "Dusk" like dialog functions, you need to add the `LegacyBrowserHandling` trait when using the `PuthDuskTestCase` class. The only difference is that there is no `$browser->typeInDialog('Hello World');` function, instead you provide the text you want to type in the `$browser->acceptDialog('prompt answer')` function.
For an example usage take a look at Puths internal InteractsWithDialogTest.php.