Here are all of the issues and their solutions that I"ve faced during setting up Laravel dusk locally and also on Github Actions pipeline.
Dusk Version 7.x
Php 8.0
On php artisan dusk I got this error Error.
RuntimeException: Invalid path to Chromedriver [/var/www/path/vendor/laravel/dusk/src/Chrome/../../bin/chromedriver-linux]. Make sure to install the Chromedriver first by running the dusk:chrome-driver command
FIX : 1st ->
Run this "chmod -R 0755 vendor/laravel/dusk/bin/" it's for permission
-Open terminal inside project dir.
-chmod -R 0755 vendor/laravel/dusk/bin/ , paste and hit.
2nd ->
if you still got the same issue try these,
1st ->
Try adding port like the image below in the DuskTestCase.php (9515 it's default port).
2nd ->
Try adding '--no-sandbox' in "DuskTestCase.php" inside driver function
protected function driver(): RemoteWebDriver
{
$options = (new ChromeOptions)->addArguments(collect([
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
])->unless($this->hasHeadlessDisabled(), function (Collection $items) {
return $items->merge([
'--disable-gpu',
'--headless=new',
'--no-sandbox' //Here--------------------
]);
})->all());
return RemoteWebDriver::create(
env('DUSK_URL') && env('DUSK_PORT') ? env('DUSK_URL').':'.env('DUSK_PORT') : 'http://127.0.0.1:9515', //Here--------------------
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
Facebook\WebDriver\Exception\Internal\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"args":["--window-size=1920,1080"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","goog:chromeOptions":{"args":["--window-size=1920,1080"]}}}
FIX :
This error occurs when Chrome Driver is not running
Now you need to run Chrome Driver, Follow below steps
Open Terminal in project root
paste this command
This should run the chrome driver on port 9515 as you can see on this SS.
If it is still not running then you can try installing ChromeDriver for linux manually by running following commands
LATEST_CHROMEDRIVER_VERSION=$(curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE)
wget -O chromedriver.zip https://chromedriver.storage.googleapis.com/$LATEST_CHROMEDRIVER_VERSION/chromedriver_linux64.zip
unzip chromedriver.zip
sudo mv chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
rm chromedriver.zip //just for cleaning
After installation, you can start ChromeDriver on a specific port (e.g., 9515) with:
chromedriver --port=9515
Note : You need to Install Chrome for linux too and also make sure that both has compatible Versions with each other.
Keep the Chrome terminal open and open new terminal and hit
php artisan ser
Make sure you sets up .env file properly make sure it atleast exists these :
APP_ENV=local
APP_URL=http://localhost:8000
SESSION_DRIVER=file
SESSION_LIFETIME=120
Note : session driver in very important when you use. ->loginAs($user)
In your test cases. If you want to make sure that if user is authenticated and successfully logged in you can use this ->assertAuthenticated()
to make sure the sessions are successfully working.
Also if you are using file session driver , make sure that Application has proper permissions.
Here is the github Pipeline yml code that runs successfully on my github actions.
- name: Run Tests
env:
DB_DATABASE: db_test
DB_USERNAME: root
DB_PASSWORD: root
run: |
php artisan key:generate --force
php artisan test
- name: Install Dusk
run: |
php artisan dusk:install && rm tests/Browser/ExampleTest.php
- name: Permissions for Chrome Driver
run: |
chmod -R 0755 vendor/laravel/dusk/bin/
- name: Renaming Chrome chromedriver-linux64 to chromedriver-linux
run: |
mv vendor/laravel/dusk/bin/chromedriver-linux64 vendor/laravel/dusk/bin/chromedriver-linux
- name: Detect Chrome Driver
run: |
php artisan dusk:chrome-driver --detect
- name: Start Chrome Driver
run: ./vendor/laravel/dusk/bin/chromedriver-linux/chromedriver --port=9515 &
- name: Run Laravel Server
run: |
php artisan serve --no-reload &
- name: Wait for Server
run: sleep 5
- name: Run Dusk Tests
env:
APP_URL: http://127.0.0.1:8000
run: |
php artisan dusk --testdox --without-tty --ansi
For debugging pipeline you can add these lines in yml for logs checking.
Clearing the logs , it should be placed before php artisan dusk
,
- name: Clear Laravel Logs
run: |
echo "" > storage/logs/laravel.log
cat storage/logs/laravel.log
Just add it at the end or after php artisan dusk
command.
- name: Capture Logs if Tests Fail
if: failure()
run: cat storage/logs/laravel.log
Now run your dusk tests or Pipeline , I hope this helps. Thanks.
This works for me
Got to this path vendor/laravel/dusk/bin/chromedriver-linux64
Rename this "chromedriver-linux64" -> "chromedriver-linux"
'--no-sandbox'
as mentioned aboveThis works locally for the pipeline this solves the issue
APP_ENV=local
APP_URL=http://localhost:8000
SESSION_DRIVER=file
SESSION_LIFETIME=120
Also I did rename too , "chromedriver-linux64" -> "chromedriver-linux"