首页 > 解决方案 > 带有参数的 http POST 到 /session 引发的 Curl 错误:{"desiredCapabilities":{"browserName":"chrome","platform":"ANY" with Selenium and PHPUnit

问题描述

我正在使用 PHPUnit 运行所有测试。创建了一个启动 Apache 实例的包装器,然后启动 Selenium 独立服务器,然后在http://localhost:4444/wd/hub创建 Chrome 远程 Webdriver 实例。这个过程 100% 的时间在我们的开发机器上运行,90% 的时间在测试服务器上运行,但有时,测试会失败:

 [exec] 1) Intranet\Pages\FinancialReportsSeleniumTest::test_changeMonthYear
 [exec] Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--window-size=1400,900","--no-sandbox","--headless"]},"goog:chromeOptions":{"args":["--window-size=1400,900","--no-sandbox","--headless"]}}}
 [exec] 
 [exec] Failed to connect to localhost port 4444: Connection refused
 [exec] 
 [exec] C:\Jenkins\jobs\Intranet-Master\workspace\vendor\facebook\webdriver\lib\Remote\HttpCommandExecutor.php:292
 [exec] C:\Jenkins\jobs\Intranet-Master\workspace\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php:126
 [exec] C:\Jenkins\jobs\Intranet-Master\workspace\phpunit\library\Intranet\Selenium.php:364
 [exec] C:\Jenkins\jobs\Intranet-Master\workspace\phpunit\library\Intranet\Selenium.php:51
 [exec] C:\Jenkins\jobs\Intranet-Master\workspace\phpunit\library\Intranet\SeleniumTestCase.php:9

如果我们重新运行测试,下次可以正常工作。

开发机器:

测试机

日志文件显示服务器已启动:

10:41:27.392 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.14.0', revision: 'aacccce0'
10:41:27.392 INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server on port 4444
10:41:28.562 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

更新 #1:我们现在将 Selenium 独立服务器作为服务启动,但仍然以相同的频率失败(每 5-10 次测试运行一次)。

更新#2:在运行任何测试之前,我们现在将 Apache 实例和远程 Web 驱动程序作为 PHPUnit 引导文件的一部分启动。我们现在还使用 fsockopen() 测试 Web 驱动程序是否正在运行(首先,在尝试启动它之前)。故障率已降至 5% 以下,但仍时有发生。奇怪的是,实际上似乎在我们添加 fsockopen() 调用后出现了改进——也许存在某种时间问题?

标签: windowsseleniumgoogle-chromephpunitselenium-chromedriver

解决方案


此错误消息...

 [exec] Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--window-size=1400,900","--no-sandbox","--headless"]},"goog:chromeOptions":{"args":["--window-size=1400,900","--no-sandbox","--headless"]}}}

...暗示在初始化Chrome 浏览器会话时引发了Curl错误。

您的主要问题似乎是将desiredCapability platform设置为ANY.


根据处理功能 - WebDriver W3C Living Document的platformName部分,以下平台名称是常用的,具有易于理解的语义,并且在匹配功能时,可以通过将它们视为知名操作系统的有效同义词来实现最大的互操作性:

Key         System
---         ------
"linux"     Any server or desktop system based upon the Linux kernel.
"mac"       Any version of Apple’s macOS.
"windows"   Any version of Microsoft Windows, including desktop and mobile versions.

注意:此列表并不详尽。

从 New Session 返回功能时,返回更具体的 platformName 是有效的,允许用户正确识别 WebDriver 实现正在运行的操作系统。

因此,与其"platform":"ANY"desiredCapabilities对象中传递,不如采用更具体"platform":"windows"的方法。


推荐阅读