# Testing
# Introduction
Rapidez is built upon Laravel (opens new window) so you have its full testing suite (opens new window) including Laravel Dusk (opens new window) 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 (opens new window) compatible browser to be installed by default. This means the system you want to run your tests on will need to have Chrome (opens new window) or Chromium (opens new window) 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 (opens new window) in Bitbucket pipelines.
For the chillio dusk image you will need to add --no-sandbox (opens new window) to the driver options (opens new window)
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 (opens new window)
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 (opens new window) for that.
# Browser tests (Dusk tests)
Dusk or Browser tests are also covered by the Laravel documentation (opens new window). 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 (opens new window) 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 (opens new window) is similar to waitUntil (opens new window) 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/