繁体   English   中英

Python中的Selenium Grid-每次第一次测试后功能测试超时

[英]Selenium Grid in Python - functional tests timeout after first test everytime

因此,我一直在尝试为django应用程序编写功能测试,并且似乎有一些奇怪的行为:每次运行测试时,Selenium都会在第一次测试后无限期挂起,只有当我重新启动集线器和节点容器时,它让我进行了另一项测试,之后再次挂起。 乍看之下,我研究了这个线程 ,似乎是相同的错误,但是即使进行了这些更改,我也遇到了相同的问题。

我的设置说明:我正在使用docker-compose,具有3个(主要)服务:django,selenium_hub和selenium_firefox。 在下面找到我的docker-compose.yml:

version: '3'
services:
  web:
    build: 
        context: django
        dockerfile: Dockerfile 
    stdin_open: true
    tty: true
    volumes:
      - ./django/my_web:/src/my_web/
    ports:
      - "8000:8000"
    networks: ["my-net"]

  nginx:
    image: nginx
    volumes: 
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./nginx/static:/usr/share/nginx/djangodocker/static
      - ./nginx/certs/:/etc/nginx/conf.d/certs:ro
    ports:
      - "80:80"
      - "443:443"
    depends_on: 
      - web
    networks: ["my-net"]

  selenium_hub:
    container_name: selenium_hub
    image: selenium/hub
    volumes:
      - /dev/shm:/dev/shm
    environment:
      - DBUS_SESSION_BUS_ADDRESS=/dev/null
    shm_size: 512MB
    ports:
      - "4444:4444"
    networks: ["my-net"]

  selenium_firefox:
    container_name: selenium_firefox
    image: selenium/node-firefox-debug
    volumes:
      - /dev/shm:/dev/shm
    shm_size: 512MB
    environment:
      - DBUS_SESSION_BUS_ADDRESS=/dev/null
      - HUB_PORT_4444_TCP_ADDR=selenium_hub
      - HUB_PORT_4444_TCP_PORT=4444
    ports:
      - "5901:5900"
    depends_on:
      - selenium_hub  
    networks: ["my-net"]

networks:
  my-net:

如您所见,我尝试挂载/ dev / shm,设置shm_size并设置环境变量:DBUS_SESSION_BUS_ADDRESS = / dev / null无济于事。

我认为问题的原因可能在于测试本身,因此我加入了以下代码:

from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class SeleniumTest(TestCase):

    def setUp(self):
        print('Setting up...')
        self.firefox = webdriver.Remote(
            command_executor='http://selenium_hub:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.FIREFOX
        )
        print('Done; connected') 

    def test_visit_site_with_firefox(self): 
        selenium = self.firefox
        selenium.get('http://web:8000')
        assert "Home" in selenium.title
        self.firefox.quit()

     .... more tests below...

然而,每当我运行manage.py test --verbosity=2我都会得到第一个测试的结果,然后它在print("Setting up...")行之后挂起,并且我等待了40分钟而没有任何响应错误信息。

我想知道是否有人在docker版本18.06.1-ce和docker-compose版本1.22.0上对所有图像使用:latest标签,从而发现或发现了Selenium中类似的行为。

我最终自己解决了这个问题。 真的很简单,我只需要在TestCase类中使用tearDown()函数即可退出Selenium,以使其继续进行下一个测试案例。

希望这可以帮助某人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM