Jenkins中使用Karate-chrome Docker运行UI测试失败求助
Karate UI测试连接localhost:9222被拒绝问题排查与解决
问题场景
在Intuit Jenkins服务器中使用karate-chrome Docker镜像运行UI测试时,出现连接拒绝错误,错误日志如下:
08:36:49 SEVERE: driver config / start failed: http call failed after 2 milliseconds for url: http://localhost:9222/json, options: {type=chrome, showDriverLog=true, start=true, executable=/usr/bin/google-chrome, target=null} 08:36:49 com.intuit.karate.KarateException: http call failed after 2 milliseconds for url: http://localhost:9222/json 08:36:49 at com.intuit.karate.core.ScenarioEngine.httpInvokeOnce(ScenarioEngine.java:599) 08:36:49 at com.intuit.karate.core.ScenarioEngine.httpInvoke(ScenarioEngine.java:558) 08:36:49 at com.intuit.karate.Http.method(Http.java:83) 08:36:49 at com.intuit.karate.Http.method(Http.java:92) 08:36:49 at com.intuit.karate.Http.get(Http.java:100) 08:36:49 Caused by: java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:9222 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
当前使用的Karate Driver配置:
configure driver = { type: 'chrome', showDriverLog: true, start: true, executable: '/usr/bin/google-chrome' }
问题原因
- karate-chrome镜像中的Chrome默认不会开启远程调试端口9222,Karate尝试连接该端口时自然被拒绝。
- 仅指定executable路径但未配置Chrome的启动参数,导致Chrome启动后未监听9222端口。
解决方案
方案1:修改Karate配置,强制Chrome开启远程调试
更新configure语句,添加args参数指定Chrome启动参数,确保开启远程调试端口:
configure driver = { type: 'chrome', showDriverLog: true, start: true, executable: '/usr/bin/google-chrome', args: ['--remote-debugging-port=9222', '--no-sandbox', '--headless=new'] }
参数说明:
--remote-debugging-port=9222:指定Chrome监听9222远程调试端口,匹配Karate默认连接地址--no-sandbox:Docker容器及Jenkins环境下需要禁用Chrome沙箱(无完整系统权限)--headless=new:启用新版无头模式,适配CI/CD无图形界面环境
方案2:连接已启动的Chrome实例(适用于Chrome独立运行场景)
若Jenkins环境中Chrome已提前启动并开启9222端口,可配置Karate跳过自动启动,直接连接现有实例:
configure driver = { type: 'chrome', showDriverLog: true, start: false, target: 'http://localhost:9222' }
注意:需确保目标Chrome实例已开启远程调试端口。
方案3:检查Docker网络配置(容器内运行场景)
如果测试在Docker容器内执行:
- 若Chrome在容器内启动:确保容器内部网络可访问localhost:9222
- 若Chrome在宿主机启动:需将容器的9222端口映射到宿主机,同时将
target配置为宿主机IP(而非localhost)
内容的提问来源于stack exchange,提问作者donny
相关产品推荐
相关产品推荐

