如何让Laravel Dusk在MacOS上使用Chrome-for-Testing而非常规Chrome
环境信息
- 系统:MacOS
- Dusk版本:8.2.5
- Laravel版本:10.35.0
- PHP版本:8.2.13
- PHPUnit版本:10.5.2
问题描述
此前一直使用Chrome-for-Testing的Chromedriver配合Laravel Dusk,以本地安装的常规Chrome作为测试浏览器。最近Chromedriver与常规Chrome版本不同步,出现无法连接到localhost的错误:
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
已知解决方案是下载版本匹配的Chrome-for-Testing(CFT)浏览器(例如128.0.6613.137版本的Chromedriver和CFT浏览器),但完成下载后,仍无法让Dusk使用该CFT浏览器。
尝试过的操作
在DuskTestCase.php的driver方法中设置浏览器二进制文件路径:
$options->setBinary('/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing');
结果仍出现curl错误:
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
其他尝试:
- 将路径改为明显错误的路径,或省略CFT浏览器二进制路径,得到的错误均完全相同。
疑问
这可能并非Chromedriver与浏览器版本不同步的问题。最初的“无法连接到localhost”错误过去是因版本不匹配导致,但设置错误路径却未触发对应异常,这点令人疑惑。
内容的提问来源于stack exchange,提问作者Ian Sweeney

