首页 > 解决方案 > 有没有办法通过我的 docker 镜像在 selenium 网格上运行我的 python 测试?

问题描述

我正在尝试通过将测试用例捆绑到 docker 映像中来运行由测试用例组成的 python 应用程序。然后在 Chrome 和 Firefox 节点上的 selenium 网格上运行它们。

通过为我的 python 应用程序构建 docker 映像,我无法成功运行我的应用程序。

1-我尝试构建我的图像,将其推送到我的 docker hub 并通过一个包含所有服务(Selenium 网格、节点、应用程序)的 docker-compose 文件检索它。

2 - 我尝试与 docker-compose 文件分开构建我的图像。将(Selenium Grid 和节点)组合起来之后;我为 python 应用程序手动构建了我的 docker 映像,并使用 {docker run [image-name]} 来运行该应用程序。

以上两种方法都不适合我。

用于 python 应用程序的 Dockerfile

FROM python: latest

WORKDIR /app

COPY ./myapp /app

RUN pip install -r /app/requirements.txt

CMD ["pytest","-n2","-s"]

Docker-撰写文件

我需要网络吗??如果没有我在这个 docker-compose 文件中创建的网络,我可以实现我想要的吗?

version: '3.7'

services:
  hub:
    image: selenium/hub:3.141.59
    networks:
      q2cnw: {}
    environment:
      - GRID_MAX_SESSION=50
      - GRID_BROWSER_TIMEOUT=60000
      - GRID_TIMEOUT=60000
      - GRID_NEW_SESSION_WAIT_TIMEOUT=60000
      - GRID_MAX_INSTANCES=3
    ports:
      - 4444:4444

  chrome:
    image: selenium/node-chrome-debug
    depends_on:
      - hub
    networks:
      q2cnw: {}
    environment:
      - HUB_PORT_4444_TCP_ADDR=hub
      - HUB_PORT_4444_TCP_PORT=4444
    ports:
      - "9001:5900"
    links:
      - hub


  firefox:
    image: selenium/node-firefox-debug
    depends_on:
      - hub
    networks:
      q2cnw: {}
    environment:
      - HUB_PORT_4444_TCP_ADDR=hub
      - HUB_PORT_4444_TCP_PORT=4444
    ports:
      - "9002:5900"
    links:
      - hub

  app:
    container_name: demo_pytest_app
    image: {docker-image-from-repository}
    networks:
      q2cnw: {}
    ports:
      - "8080"

networks:
  q2cnw:
    driver: bridge

我的 conftest.py 文件

根据上面的 docker-compose,URL 是否有任何更改?

    driver = webdriver.Remote(
        command_executor="http://127.0.0.1:4444/wd/hub",
        desired_capabilities=DesiredCapabilities.FIREFOX
    )

预期结果:(这表明它运行了我的浏览器和引发预期错误的应用程序;我很好)

        try:
            driver.find_element_by_xpath("//div[@class='alert alert-danger alert-dismissible']")
>           raise Exception("ERROR MESSAGE BANNER PROBLEM")
E           Exception: ERROR MESSAGE BANNER PROBLEM

实际结果:

(我使用此命令运行 pytest 映像 -> [docker run -it pytest-image])

以下错误表明连接因主机 URL 而被拒绝。有谁知道当我们尝试通过 docker 图像将我们的应用程序连接到 selenium grid + 节点时发生的这个错误?

E           urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnect
ionError('<urllib3.connection.HTTPConnection object at 0x7f29f02f2a10>: Failed to establish a new connection: [Errno 111] Connection refused'))

标签: python-3.xdocker-composedockerfilepytestselenium-grid

解决方案


你可以试试这个图像 - https://hub.docker.com/repository/docker/benjose22/selenium_pytest_remote

如何运行 -第 1 步:根据https://github.com/SeleniumHQ/docker-selenium#via-docker-compose
 旋转 Docker-compose  第 2 步:
docker run --network="host" -it -v ~/host_scripts:/src benjose22/selenium_pytest_remote:v1.0 test_sample.py

conftest.py可以像 -

        caps = {'browserName': os.getenv('BROWSER', 'firefox')}<br/>
        self.browser = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=caps)

推荐阅读