首页 > 解决方案 > 测试运行器节点看不到 selenium-hub。出现错误:ECONNREFUSED 连接 ECONNREFUSED 127.0.0.1:4444

问题描述

简而言之,有一个额外的容器作为无法到达 selenium-hub 的测试运行器。因此,测试失败。

添加此容器以使用云构建在云上运行测试。

我创建了 docker-compose 如下:

 version: "3"
    services:
      selenium-hub:
        image: selenium/hub:4.0.0-rc-1-prerelease-20210804
        container_name: selenium-hub
        ports:
          - "4444:4444"
        expose: 
          - 4444
    
      chrome:
        image: selenium/node-chrome:4.0.0-rc-1-prerelease-20210804
        shm_size: 2gb
        depends_on:
          - selenium-hub
        environment:
          - SE_EVENT_BUS_HOST=selenium-hub
          - SE_EVENT_BUS_PUBLISH_PORT=4442
          - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
        ports:
          - "6900:5900"
    
      chrome_video:
        image: selenium/video:ffmpeg-4.3.1-20210804
        volumes:
          - /Users/videos:/videos 
        depends_on:
          - chrome
        environment:
          - DISPLAY_CONTAINER_NAME=chrome
          - FILE_NAME=chrome_video.mp4

容器开始成功运行后,当我npm run test运行 selenium js 测试时,我在预期目录中获得了成功的结果和视频录制。但它也应该是自动化的。npm run test应该以某种方式触发。

在我们的 CI/CD 过程中,添加了 cloudbuild.yaml 文件以便在云上运行。

 steps:
   - name: 'docker/compose:1.29.2'
      args: ['run','test']

   - name: 'docker/compose:1.29.2'
      args: ['stop']
      timeout: 60s

云构建应该触发下面的新容器,该容器作为测试运行器添加到 docker-compose 文件中:

test:
    image:  node:16-alpine
    entrypoint:
    - sh
    - -c
    - |-
      cd /test
      npm install
      sleep 3
      npm run test
    volumes:
    - .:/test
    depends_on:
    - selenium
    network_mode: host

但是对于测试容器,测试失败并出现以下错误:

24 packages are looking for funding


run `npm fund` for details


2 moderate severity vulnerabilities


To address all issues, run:

npm audit fix


Run `npm audit` for details.


> js_mocha_selenium@1.0.0 test

> mocha test




Preliminary steps for End to End Tests

initalising the session...

1) Login

closing the session...

2) "after each" hook for "Login"



0 passing (108ms)

2 failing


1) Preliminary steps for End to End Tests

Login:

Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:4444

at ClientRequest.<anonymous> (node_modules/selenium-webdriver/http/index.js:273:15)

at ClientRequest.emit (node:events:394:28)

at Socket.socketErrorListener (node:_http_client:447:9)

at Socket.emit (node:events:394:28)

at emitErrorNT (node:internal/streams/destroy:157:8)

at emitErrorCloseNT (node:internal/streams/destroy:122:3)

at processTicksAndRejections (node:internal/process/task_queues:83:21)


2) Preliminary steps for End to End Tests

"after each" hook for "Login":

Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:4444

at ClientRequest.<anonymous> (node_modules/selenium-webdriver/http/index.js:273:15)

at ClientRequest.emit (node:events:394:28)

at Socket.socketErrorListener (node:_http_client:447:9)

at Socket.emit (node:events:394:28)

at emitErrorNT (node:internal/streams/destroy:157:8)

at emitErrorCloseNT (node:internal/streams/destroy:122:3)

at processTicksAndRejections (node:internal/process/task_queues:83:2

容器:

CONTAINER ID   IMAGE                                                 COMMAND                   CREATED              STATUS              PORTS                                                      NAMES
7ca30366bc09   node:16-alpine                                        "sh -c 'cd /test\nnpm…&quot;   About a minute ago   Up About a minute                                                              e2e-tests_test_1
fdf43be1b4da   selenium/video:ffmpeg-4.3.1-20210804                  "/opt/bin/entry_poin…&quot;    16 minutes ago       Up About a minute   9000/tcp                                                   e2e-tests_chrome_video_1
92c023b15cb6   selenium/node-chrome:4.0.0-rc-1-prerelease-20210804   "/opt/bin/entry_poin…&quot;    16 minutes ago       Up About a minute   0.0.0.0:6900->5900/tcp, :::6900->5900/tcp                  e2e-tests_chrome_1
86002f3d1eb9   selenium/hub:4.0.0-rc-1-prerelease-20210804           "/opt/bin/entry_poin…&quot;    16 minutes ago       Up About a minute   4442-4443/tcp, 0.0.0.0:4444->4444/tcp, :::4444->4444/tcp   selenium-hub 

我可以从e2e-tests_test_1容器 ping selenuim -hub,但不能反向执行(从selenium-hub ping e2e-tests_test_1)。

关于当前网络:

 >> % docker network inspect -v host
[
    {
        "Name": "host",
        "Id": "36e4060f18be618399692294d10cf6be3478c1bf5190ea035b002ca87c18276b",
        "Created": "2021-06-30T10:36:33.170635189Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

似乎测试节点无法到达127.0.0.1:4444

我应该怎么做才能解决这个问题?很高兴听到替代解决方案。

提前致谢。

标签: seleniumdocker-composegoogle-cloud-builddocker-networkingselenium-docker

解决方案


在运行测试之前,您需要等待 Grid 准备好。我在项目的自述文件中记录了一些方法,请查看https://github.com/seleniumhq/docker-selenium/#waiting-for-the-grid-to-be-ready


推荐阅读