我试图在docker容器中运行Selenium测试,但是得到
错误:ECONNREFUSED连接ECONNREFUSED 127.0.0.1:4444
docker-compose.yml
version: "3"
services:
chrome:
image: selenium/standalone-chrome-debug
ports:
- "4444:4444"
webtest:
build: .
testfile.js
var sw = require('selenium-webdriver');
let driver = new sw.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub')
.build();
driver.get('https://www.google.com/');
当我在容器外部运行testfile.js时,它正在工作。只能在容器内运行面临的问题
答案 0 :(得分:1)
您需要将“ http://localhost:4444/wd/hub”替换为“ http://chrome:4444/wd/hub”,并配置Docker网络。容器“ localhost”内部不是您的主机,因此该端口不可访问并拒绝连接。端口映射仅在容器和主机之间适用。
至少在容器之间创建链接(请参见下文),更好地直接配置网络 (https://docs.docker.com/compose/networking/)
version: "3"
services:
chrome:
image: selenium/standalone-chrome-debug
ports:
- "4444:4444"
webtest:
build: .
links:
- chrome