Search code examples
laravellaravel-10laravel-dusklaravel-dusk2

How to tell Laravel Dusk to use Chrome-for-testing (CFT) browser rather than the regular installed version of Chrome


On MacOs

Dusk Version -8.2.5

Laravel Version - 10.35.0

PHP Version - 8.2.13

PHPUnit Version - 10.5.2

I've been using the Chromedriver from Chrome-for-Testing with Laravel Dusk to test our app. Until recently I had been testing against the version of the regular Chrome browser installed on my machine. That worked fine until the version of chromedriver available was out of sync with the version of my regular chrome browser and I got the dreaded Failed to connect to localhost error

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"]}}}
Failed to connect to localhost port 9515 after 1 ms: Couldn't connect to server

The answer as I understand it is to grab the matching version of CfT browser to test against, e.g. get the 128.0.6613.137 ChromeDriver and 128.0.6613.137 CfT browser.

I did that but I have not been able to get Dusk to use that CfT browser.

What I tried

I set the Binary location (which I think is to the CfT browser location as opposed to the Chromedriver location) to DuskTestCase.php > driver method;

$options->setBinary('/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing');

The result (error)

All my tests still fail with a curl error;

Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing","args":["--window-size=1920,1080"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","goog:chromeOptions":{"binary":"/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing","args":["--window-size=1920,1080"]}}}
Failed to connect to localhost port 9515 after 0 ms: Couldn't connect to server

Other things I tried

I tried changing the path to an obviously incorrect path and I get the same error. I tried omitting the path to the CfT browser binary and I get the same error

Other thoughts

This might not be an issue of chromedriver and browser being out of sync. This started with the "The failed to connect to localhost" error and in the past that was because the chromedriver and installed chrome browser were out of sync. I'm assuming that if I get the ChromeDriver and Chrome Browser in sync the issue will be resolved. But its a bit strange that when I set an obviously incorrect path to the CfT browser I dont get an error.


Solution

  • I too had been facing the same issue with my dusk tests. After a lot of looking around I finally found this PR on the repo: https://github.com/laravel/dusk/pull/1124.

    In my DuskTestCase.php I am manually calling the static::startChromeDriver() so I needed to implement the same change in my call and specify the port: static::startChromeDriver(['--port=9515']);

    Not sure if this is the same for you, but might be something to look at.