Skip to content

Commit 98b2b2e

Browse files
committed
Update Dusk Documentation
Added new option on how to setup and run Dusk tests without Selenium.
1 parent a2edf57 commit 98b2b2e

1 file changed

Lines changed: 188 additions & 7 deletions

File tree

DOCUMENTATION/content/guides/index.md

Lines changed: 188 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,195 @@ Assuming that you are in laradock folder, type:
565565
<a name="Laravel-Dusk"></a>
566566
# Running Laravel Dusk Tests
567567

568+
- [Option 1: Without Selenium](#option1-dusk)
569+
- [Option 2: With Selenium](#option2-dusk)
570+
571+
<a name="option1-dusk"></a>
572+
## Option 1: Without Selenium
573+
574+
- [Intro](#option1-dusk-intro)
575+
- [Workspace Setup](#option1-workspace-setup)
576+
- [Application Setup](#option1-application-setup)
577+
- [Choose Chrome Driver Version (Optional)](#option1-choose-chrome-driver-version)
578+
- [Run Dusk Tests](#option1-run-dusk-tests)
579+
580+
<a name="option1-dusk-intro"></a>
581+
### Intro
582+
583+
This is a guide to run Dusk tests in your `workspace` container with headless
584+
google-chrome and chromedriver. It has been tested with Laravel 5.4 and 5.5.
585+
586+
<a name="option1-workspace-setup"></a>
587+
### Workspace Setup
588+
589+
Update your .env with following entries:
590+
591+
```
592+
...
593+
# Install Laravel installer bin to setup demo app
594+
WORKSPACE_INSTALL_LARAVEL_INSTALLER=true
595+
...
596+
# Install all the necessary dependencies for running Dusk tests
597+
WORKSPACE_INSTALL_DUSK_DEPS=true
598+
...
599+
```
600+
601+
Then run below to build your workspace.
602+
603+
```
604+
docker-compose build workspace
605+
```
606+
607+
<a name="option1-application-setup"></a>
608+
### Application Setup
609+
610+
Run a `workspace` container and you will be inside the container at `/var/www` directory.
611+
612+
```
613+
docker-compose run workspace bash
614+
615+
/var/www#> _
616+
```
617+
618+
Create new Laravel application named `dusk-test` and install Laravel Dusk package.
619+
620+
```
621+
/var/www> laravel new dusk-test
622+
/var/www> cd dusk-test
623+
/var/www/dusk-test> composer require --dev laravel/dusk
624+
/var/www/dusk-test> php artisan dusk:install
625+
```
626+
627+
Create `.env.dusk.local` by copying from `.env` file.
628+
629+
```
630+
/var/www/dusk-test> cp .env .env.dusk.local
631+
```
632+
633+
Update the `APP_URL` entry in `.env.dusk.local` to local Laravel server.
634+
635+
```
636+
APP_URL=http://localhost:8000
637+
```
638+
639+
You will need to run chromedriver with `headless` and `no-sandbox` flag. In Laravel Dusk 2.x it is
640+
already set `headless` so you just need to add `no-sandbox` flag. If you on previous version 1.x,
641+
you will need to update your `DustTestCase#driver` as shown below.
642+
643+
644+
```
645+
<?php
646+
647+
...
648+
649+
abstract class DuskTestCase extends BaseTestCase
650+
{
651+
...
652+
653+
/**
654+
* Update chrome driver with below flags
655+
*/
656+
protected function driver()
657+
{
658+
$options = (new ChromeOptions)->addArguments([
659+
'--disable-gpu',
660+
'--headless',
661+
'--no-sandbox'
662+
]);
663+
664+
return RemoteWebDriver::create(
665+
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
666+
ChromeOptions::CAPABILITY, $options
667+
)
668+
);
669+
}
670+
}
671+
```
672+
673+
<a name="option1-choose-chrome-driver-version"></a>
674+
### Choose Chrome Driver Version (Optional)
675+
676+
You could choose to use either:
677+
678+
1. Chrome Driver shipped with Laravel Dusk. (Default)
679+
2. Chrome Driver installed in `workspace` container. (Required tweak on DuskTestCase class)
680+
681+
For Laravel 2.x, you need to update `DuskTestCase#prepare` method if you wish to go with option #2.
682+
683+
```
684+
685+
<?php
686+
687+
...
688+
abstract class DuskTestCase extends BaseTestCase
689+
{
690+
...
691+
public static function prepare()
692+
{
693+
// Only add this line if you wish to use chrome driver installed in workspace container.
694+
// You might want to read the file path from env file.
695+
static::useChromedriver('/usr/local/bin/chromedriver');
696+
697+
static::startChromeDriver();
698+
}
699+
```
700+
701+
For Laravel 1.x, you need to add `DuskTestCase#buildChromeProcess` method if you wish to go with option #2.
702+
703+
```
704+
<?php
705+
706+
...
707+
use Symfony\Component\Process\ProcessBuilder;
708+
709+
abstract class DuskTestCase extends BaseTestCase
710+
{
711+
...
712+
713+
/**
714+
* Only add this method if you wish to use chrome driver installed in workspace container
715+
*/
716+
protected static function buildChromeProcess()
717+
{
718+
return (new ProcessBuilder())
719+
->setPrefix('chromedriver')
720+
->getProcess()
721+
->setEnv(static::chromeEnvironment());
722+
}
723+
724+
...
725+
}
726+
```
727+
728+
<a name="option1-run-dusk-tests"></a>
729+
### Run Dusk Tests
730+
731+
Run local server in `workspace` container and run Dusk tests.
732+
733+
```
734+
# alias to run Laravel server in the background (php artisan serve --quiet &)
735+
/var/www/dusk-test> serve
736+
# alias to run Dusk tests (php artisan dusk)
737+
/var/www/dusk-test> dusk
738+
739+
PHPUnit 6.4.0 by Sebastian Bergmann and contributors.
740+
741+
. 1 / 1 (100%)
742+
743+
Time: 837 ms, Memory: 6.00MB
744+
```
745+
746+
<a name="option2-dusk"></a>
747+
## Option 2: With Selenium
748+
568749
- [Intro](#dusk-intro)
569750
- [DNS Setup](#dns-setup)
570751
- [Docker Compose Setup](#docker-compose)
571752
- [Laravel Dusk Setup](#laravel-dusk-setup)
572753
- [Running Laravel Dusk Tests](#running-tests)
573754

574755
<a name="dusk-intro"></a>
575-
## Intro
756+
### Intro
576757
Setting up Laravel Dusk tests to run with Laradock appears be something that
577758
eludes most Laradock users. This guide is designed to show you how to wire them
578759
up to work together. This guide is written with macOS and Linux in mind. As such,
@@ -583,7 +764,7 @@ This guide assumes you know how to use a DNS forwarder such as `dnsmasq` or are
583764
with editing the `/etc/hosts` file for one-off DNS changes.
584765

585766
<a name="dns-setup"></a>
586-
## DNS Setup
767+
### DNS Setup
587768
According to RFC-2606, only four TLDs are reserved for local testing[^1]:
588769

589770
- `.test`
@@ -617,7 +798,7 @@ This will ensure that when navigating to `myapp.test`, it will route the
617798
request to `127.0.0.1` which will be handled by Nginx in Laradock.
618799

619800
<a name="docker-compose"></a>
620-
## Docker Compose setup
801+
### Docker Compose setup
621802
In order to make the Selenium container talk to the Nginx container appropriately,
622803
the `docker-compose.yml` needs to be edited to accommodate this. Make the following
623804
changes:
@@ -640,7 +821,7 @@ necessary for running Dusk tests. These changes also link the `nginx` environmen
640821
variable to the domain you wired up in your hosts file.
641822

642823
<a name="laravel-dusk-setup"></a>
643-
## Laravel Dusk Setup
824+
### Laravel Dusk Setup
644825

645826
In order to make Laravel Dusk make the proper request to the Selenium container,
646827
you have to edit the `DuskTestCase.php` file that's provided on the initial
@@ -650,13 +831,13 @@ Remote Web Driver attempts to use to set up the Selenium session.
650831
One recommendation for this is to add a separate config option in your `.env.dusk.local`
651832
so it's still possible to run your Dusk tests locally should you want to.
652833

653-
### .env.dusk.local
834+
#### .env.dusk.local
654835
```
655836
...
656837
USE_SELENIUM=true
657838
```
658839
659-
### DuskTestCase.php
840+
#### DuskTestCase.php
660841
```php
661842
abstract class DuskTestCase extends BaseTestCase
662843
{
@@ -677,7 +858,7 @@ abstract class DuskTestCase extends BaseTestCase
677858
```
678859

679860
<a name="running-tests"></a>
680-
## Running Laravel Dusk Tests
861+
### Running Laravel Dusk Tests
681862

682863
Now that you have everything set up, to run your Dusk tests, you have to SSH
683864
into the workspace container as you normally would:

0 commit comments

Comments
 (0)