如何从另一个容器连接到独立的 selenium-firefox 容器
how to connect to standalone selenium-firefox container from another container
我正在尝试 运行 我的自动化 GUI 测试套件,它位于我的自动化容器中。
我在端口 4444 上单独下载了独立的 selenium-firefox 映像和 运行ning 作为容器 运行ning。运行ning 在
localhost:4444/wd/hub
现在,我正在尝试 运行 我的自动化容器中的测试套件,我正在使用以下代码 运行 在无头模式下在我的机器人框架中使用 selenium 的 firefox 版本测试套件。
Open Browser http://www.youtube.com headlessfirefox remote_url=http://localhost:4444/wd/hub
当我从我的机器 运行 我的测试套件时,这种方法工作正常,当我 运行 它在自动化容器中时,它失败了。
有什么方法可以让我的自动化容器到达 selenium-firefox 容器以使用浏览器。
错误:
C: 10: Open Chrome headless
/usr/local/lib/python3.6/site-packages/requests/__init__.py:91:
RequestsDependencyWarning: urllib3 (1.26.3) or chardet (3.0.4) doesn't
match a supported version!
RequestsDependencyWarning)
[ WARN ] Retrying (Retry(total=2, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a322440b8>: Failed to establish a
[ WARN ] Retrying (Retry(total=1, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32244710>: Failed to establish a
new connection: [Errno 111] Connection refused',)': /wd/hub/session
[ WARN ] Retrying (Retry(total=0, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32235710>: Failed to establish a
new connection: [Errno 111] Connection refused',)': /wd/hub/session
| FAIL |
MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max
retries exceeded with url: /wd/hub/session (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32235438>: Failed to establish a new connection: [Errno 111]
Connection refused',))
如有帮助将不胜感激
您需要创建 docker-compose.yml 文件,其中包含您要创建的所有容器:
version: '3.8'
services:
chrome:
image: selenium/standalone-chrome:85.0
hostname: chrome
ports:
- "4444:4444"
e2e-tests:
build: .
depends_on:
- chrome
并在容器内使用主机名 'chrome' 将如何使用它:
cls.driver = webdriver.Remote(command_executor='http://chrome:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
您的容器在网络方面被视为不同的机器。当您启动 selenium-firefox 图像时,您可能 link 容器的端口 4444 与主机的端口 4444。然后,当您在主机上请求 localhost:4444 时,它 link 发送到您的容器。
但是当你启动另一个容器时,localhost:4444表示这个容器的4444端口!
你应该做的是使用嵌入的 dns 机制 docker 提供:当你创建一个容器时,它有一个名称(你可以指定)。 Docker 在默认 docker 网络 上给它一个内部 ip,但不会 将容器名称与该 ip 相关联。为了能够在您的网址中使用容器名称作为域名,您需要创建一个新网络,然后它将使用嵌入 docker dns 并将其自己的名称作为别名。
这就是 Vova 答案有效的原因:docker-compose 默认为您的容器创建一个网络,这意味着它们会自动获得一个别名。我太习惯于使用网络 and/or 组合,以至于我忽略了一个事实,即默认 docker 网络上的别名不是自动的。
您应该仅为这些容器创建特定的桥接网络:docker network create selenium-net --driver bridge
然后docker run -p 4444:4444 --name selenium --network selenium-net selenium/standalone-firefox:latest
当你运行你的其他容器时,你也应该将它们添加到selenium-net
网络
另一种方法是将您已有的容器连接到网络:假设我有容器 selenium
和 testsuite
运行ning(或现有容器)
我将 运行 docker network connect selenium-net selenium
和 docker network connect selenium-net testsuite
放在网络中。当你这样做时,如果你想在 docker 网络上给他们一个特定的 alias/domain,你可以指定 --alias
选项。
我正在尝试 运行 我的自动化 GUI 测试套件,它位于我的自动化容器中。
我在端口 4444 上单独下载了独立的 selenium-firefox 映像和 运行ning 作为容器 运行ning。运行ning 在
localhost:4444/wd/hub
现在,我正在尝试 运行 我的自动化容器中的测试套件,我正在使用以下代码 运行 在无头模式下在我的机器人框架中使用 selenium 的 firefox 版本测试套件。
Open Browser http://www.youtube.com headlessfirefox remote_url=http://localhost:4444/wd/hub
当我从我的机器 运行 我的测试套件时,这种方法工作正常,当我 运行 它在自动化容器中时,它失败了。
有什么方法可以让我的自动化容器到达 selenium-firefox 容器以使用浏览器。
错误:
C: 10: Open Chrome headless
/usr/local/lib/python3.6/site-packages/requests/__init__.py:91:
RequestsDependencyWarning: urllib3 (1.26.3) or chardet (3.0.4) doesn't
match a supported version!
RequestsDependencyWarning)
[ WARN ] Retrying (Retry(total=2, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a322440b8>: Failed to establish a
[ WARN ] Retrying (Retry(total=1, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32244710>: Failed to establish a
new connection: [Errno 111] Connection refused',)': /wd/hub/session
[ WARN ] Retrying (Retry(total=0, connect=None, read=None, redirect=None,
status=None)) after connection broken by
'NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32235710>: Failed to establish a
new connection: [Errno 111] Connection refused',)': /wd/hub/session
| FAIL |
MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max
retries exceeded with url: /wd/hub/session (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f4a32235438>: Failed to establish a new connection: [Errno 111]
Connection refused',))
如有帮助将不胜感激
您需要创建 docker-compose.yml 文件,其中包含您要创建的所有容器:
version: '3.8'
services:
chrome:
image: selenium/standalone-chrome:85.0
hostname: chrome
ports:
- "4444:4444"
e2e-tests:
build: .
depends_on:
- chrome
并在容器内使用主机名 'chrome' 将如何使用它:
cls.driver = webdriver.Remote(command_executor='http://chrome:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
您的容器在网络方面被视为不同的机器。当您启动 selenium-firefox 图像时,您可能 link 容器的端口 4444 与主机的端口 4444。然后,当您在主机上请求 localhost:4444 时,它 link 发送到您的容器。
但是当你启动另一个容器时,localhost:4444表示这个容器的4444端口!
你应该做的是使用嵌入的 dns 机制 docker 提供:当你创建一个容器时,它有一个名称(你可以指定)。 Docker 在默认 docker 网络 上给它一个内部 ip,但不会 将容器名称与该 ip 相关联。为了能够在您的网址中使用容器名称作为域名,您需要创建一个新网络,然后它将使用嵌入 docker dns 并将其自己的名称作为别名。
这就是 Vova 答案有效的原因:docker-compose 默认为您的容器创建一个网络,这意味着它们会自动获得一个别名。我太习惯于使用网络 and/or 组合,以至于我忽略了一个事实,即默认 docker 网络上的别名不是自动的。
您应该仅为这些容器创建特定的桥接网络:docker network create selenium-net --driver bridge
然后docker run -p 4444:4444 --name selenium --network selenium-net selenium/standalone-firefox:latest
当你运行你的其他容器时,你也应该将它们添加到selenium-net
网络
另一种方法是将您已有的容器连接到网络:假设我有容器 selenium
和 testsuite
运行ning(或现有容器)
我将 运行 docker network connect selenium-net selenium
和 docker network connect selenium-net testsuite
放在网络中。当你这样做时,如果你想在 docker 网络上给他们一个特定的 alias/domain,你可以指定 --alias
选项。