Testing
Introduction
Rapidez is built upon Laravel so you have its full testing suite including Laravel Dusk available to you.
Dusk is most likely what you'll be using to write your tests.
Installation
Rapidez already has some tests included in the core. To install and configure dusk, and to use these tests you can run the following command.
php artisan rapidez:install:tests
Note
Laravel Dusk expects a ChromeDriver compatible browser to be installed by default. This means the system you want to run your tests on will need to have Chrome or Chromium installed.
Configuring the driver
php artisan rapidez:install:tests
already installs and configures the chromedriver to be used for testing. But we have noticed some issues in running the driver in some cases, like using this image in Bitbucket pipelines.
For the chillio dusk image you will need to add --no-sandbox to the driver options
On Bitbucket (and other pipelines/actions) we have noticed the connection to the chromedriver times out sometimes. This can be fixed by raising the timeout for the RemoteWebDriver
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
- )
+ ),
+ 35000, // Connection Timeout
+ 90000 // Request Timeout
);
Writing tests
After the command has finished you'll find that your /tests/
folder contains Browser
and Feature
.
Feature tests
The feature tests work exactly like Laravel does so we suggest looking through the testing documentation for that.
Browser tests (Dusk tests)
Dusk or Browser tests are also covered by the Laravel documentation. However we do have some extras to aid in testing Rapidez.
Additional functions
To aid in testing rapidez we have added extra functions to Dusk.
WaitUntilIdle
$browser->waitUntilIdle(120);
Since Rapidez has many API calls and code running in the background, sometimes we want to make sure these requests have completed and the browser is idling and no longer executing code. This is what waitUntilIdle is for.
It will wait until no network request is active and the browser being idle for 500ms.
Argument | default | Description |
---|---|---|
1) timeout | 120 | The timeout in seconds, if this is exceeded the test will fail. |
waitUntilTrueForDuration
$browser->waitUntilTrueForDuration('true', 120, 0.5);
waitUntilTrueForDuration is similar to waitUntil however since sometimes the result of an expression you pass can be unstable and change to false quickly after it was true we have created a function to wait until it is true for a duration.
Argument | default | Description |
---|---|---|
1) script | "true" | The javascript expression that needs to be true to continue. |
2) timeout | 120 | The timeout in seconds, if this is exceeded the test will fail. |
3) for | 0.5 | How long in seconds the expression needs to be true for before continuing. |
Additional info
Bitbucket
When using Bitbucket pipelines we recommend to use an after-script to upload the dusk result to Bitbucket.
- step:
name: Dusk Tests
services:
- chrome
artifacts:
paths:
- .bitbucket/dusk_result/**
script:
- php artisan dusk:chrome-driver --detect
- php artisan serve --host=0.0.0.0 --port=80 > /dev/null 2>&1 &
- php artisan dusk --log-junit ./test-reports/junit.xml
after-script:
- if [ $BITBUCKET_EXIT_CODE -eq 0 ]; then exit 0; fi
- mkdir -p .bitbucket/dusk_result/
- cp -r tests/Browser/screenshots/ .bitbucket/dusk_result/
- cp -r tests/Browser/console/ .bitbucket/dusk_result/
- cp -r storage/logs/ .bitbucket/dusk_result/