Skip to content

Commit 586a225

Browse files
authored
Merge pull request laradock#1089 from ELD/add-dusk-support-and-docs
add docs on running Laravel Dusk tests
2 parents 245ddf3 + c099a51 commit 586a225

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

DOCUMENTATION/content/guides/index.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ weight: 4
88

99
* [Production Setup on Digital Ocean](#Digital-Ocean)
1010
* [PHPStorm XDebug Setup](#PHPStorm-Debugging)
11+
* [Running Laravel Dusk Test](#Laravel-Dusk)
1112

1213

1314

@@ -555,4 +556,149 @@ Assuming that you are in laradock folder, type:
555556
- ![ConnectionSSHAuth](/images/photos/KiTTY/ConnectionSSHAuth.png)
556557
- ![TerminalShell](/images/photos/KiTTY/TerminalShell.png)
557558

559+
<br>
560+
<br>
561+
<br>
562+
<br>
563+
<br>
564+
565+
<a name="Laravel-Dusk"></a>
566+
# Running Laravel Dusk Tests
567+
568+
- [Intro](#dusk-intro)
569+
- [DNS Setup](#dns-setup)
570+
- [Docker Compose Setup](#docker-compose)
571+
- [Laravel Dusk Setup](#laravel-dusk-setup)
572+
- [Running Laravel Dusk Tests](#running-tests)
573+
574+
<a name="dusk-intro"></a>
575+
## Intro
576+
Setting up Laravel Dusk tests to run with Laradock appears be something that
577+
eludes most Laradock users. This guide is designed to show you how to wire them
578+
up to work together. This guide is written with macOS and Linux in mind. As such,
579+
it's only been tested on macOS. Feel free to create pull requests to update the guide
580+
for Windows-specific instructions.
581+
582+
This guide assumes you know how to use a DNS forwarder such as `dnsmasq` or are comfortable
583+
with editing the `/etc/hosts` file for one-off DNS changes.
584+
585+
<a name="dns-setup"></a>
586+
## DNS Setup
587+
According to RFC-2606, only four TLDs are reserved for local testing[^1]:
588+
589+
- `.test`
590+
- `.example`
591+
- `.invalid`
592+
- `.localhost`
593+
594+
A common TLD used for local development is `.dev`, but newer versions of Google
595+
Chrome (such as the one bundled with the Selenium Docker image), will fail to
596+
resolve that DNS as there will appear to be a name collision.
597+
598+
The recommended extension is `.test` for your Laravel web apps because you're
599+
running tests. Using a DNS forwarder such as `dnsmasq` or by editing the `/etc/hosts`
600+
file, configure the host to point to `localhost`.
601+
602+
For example, in your `/etc/hosts` file:
603+
```
604+
##
605+
# Host Database
606+
#
607+
# localhost is used to configure the loopback interface
608+
# when the system is booting. Do not change this entry.
609+
##
610+
127.0.0.1 localhost
611+
255.255.255.255 broadcasthost
612+
::1 localhost
613+
127.0.0.1 myapp.test
614+
```
615+
616+
This will ensure that when navigating to `myapp.test`, it will route the
617+
request to `127.0.0.1` which will be handled by Nginx in Laradock.
618+
619+
<a name="docker-compose"></a>
620+
## Docker Compose setup
621+
In order to make the Selenium container talk to the Nginx container appropriately,
622+
the `docker-compose.yml` needs to be edited to accommodate this. Make the following
623+
changes:
624+
625+
```yaml
626+
...
627+
selenium:
628+
...
629+
depends_on:
630+
- nginx
631+
links:
632+
- nginx:<your_domain>
633+
```
634+
635+
This allows network communication between the Nginx and Selenium containers
636+
and it also ensures that when starting the Selenium container, the Nginx
637+
container starts up first unless it's already running. This allows
638+
the Selenium container to make requests to the Nginx container, which is
639+
necessary for running Dusk tests. These changes also link the `nginx` environment
640+
variable to the domain you wired up in your hosts file.
641+
642+
<a name="laravel-dusk-setup"></a>
643+
## Laravel Dusk Setup
644+
645+
In order to make Laravel Dusk make the proper request to the Selenium container,
646+
you have to edit the `DuskTestCase.php` file that's provided on the initial
647+
installation of Laravel Dusk. The change you have to make deals with the URL the
648+
Remote Web Driver attempts to use to set up the Selenium session.
649+
650+
One recommendation for this is to add a separate config option in your `.env.dusk.local`
651+
so it's still possible to run your Dusk tests locally should you want to.
652+
653+
### .env.dusk.local
654+
```
655+
...
656+
USE_SELENIUM=true
657+
```
658+
659+
### DuskTestCase.php
660+
```php
661+
abstract class DuskTestCase extends BaseTestCase
662+
{
663+
...
664+
protected function driver()
665+
{
666+
if (env('USE_SELENIUM', 'false') == 'true') {
667+
return RemoteWebDriver::create(
668+
'http://selenium:4444/wd/hub', DesiredCapabilities::chrome()
669+
);
670+
} else {
671+
return RemoteWebDriver::create(
672+
'http://localhost:9515', DesiredCapabilities::chrome()
673+
);
674+
}
675+
}
676+
}
677+
```
678+
679+
<a name="running-tests"></a>
680+
## Running Laravel Dusk Tests
681+
682+
Now that you have everything set up, to run your Dusk tests, you have to SSH
683+
into the workspace container as you normally would:
684+
```docker-compose exec --user=laradock workspace bash```
685+
686+
Once inside, you can change directory to your application and run:
687+
688+
```php artisan dusk```
689+
690+
One way to make this easier from your project is to create a helper script. Here's one such example:
691+
```bash
692+
#!/usr/bin/env sh
693+
694+
LARADOCK_HOME="path/to/laradock"
695+
696+
pushd ${LARADOCK_HOME}
697+
698+
docker-compose exec --user=laradock workspace bash -c "cd my-project && php artisan dusk && exit"
699+
```
700+
701+
This invokes the Dusk command from inside the workspace container but when the script completes
702+
execution, it returns your session to your project directory.
558703

704+
[^1]: [Don't Use .dev for Development](https://iyware.com/dont-use-dev-for-development/)

0 commit comments

Comments
 (0)