django - 将Selenium与Pytest结合使用以在Docker中运行的Django 2.2项目

标签 django selenium docker pytest

我正在尝试将Pytest与Django插件和Selenium一起使用,以测试在Docker中运行的Django 2.2项目,但无法使Selenium连接到测试服务器。当我尝试使用以下设置进行连接时,Selenium始终返回“地址不可用”。

我的Docker撰写为:

version: '3'

volumes:
  local_postgres_data: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: local_django
    depends_on:
      - postgres
    volumes:
      - .:/app
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    ports:
      - "8000:8000"
    links:
      - selenium
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/local/postgres/Dockerfile
    image: local_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
    env_file:
      - ./.envs/.local/.postgres

  selenium:
    image: selenium/standalone-firefox
    ports:
      - "4444:4444"

我已经在DJANGO_LIVE_TEST_SERVER_ADDRESS=django env文件中定义了.django,并在conftest.py中为Selenium远程Webdriver设置了一个固定装置:
import environ
import pytest

from selenium.webdriver import Remote
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


env = environ.Env()


@pytest.fixture(scope='session')
def selenium() -> Remote:
    driver = Remote(
        command_executor=env('SELENIUM_HOST', default='http://selenium:4444/wd/hub'),
        desired_capabilities=DesiredCapabilities.FIREFOX
    )
    yield driver

我的测试用例,使用了我的selenium固定装置和Django-PyTest提供的live_server固定装置:
import pytest

class TestDashboard:
    def test_loads(self, selenium, live_server):
        selenium.get(live_server.url)
        assert My Site' in selenium.title

OSError: [Errno 99] Address not available构造函数中引发live_server

除其他事项外,我还尝试过直接使用StaticLiveServerTestCase类,省略了live_server和我的selenium固定装置,但这并没有更好地工作:
import pytest

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium.webdriver import Remote
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


pytestmark = pytest.mark.django_db


class TestDashboardView(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        cls.host = 'django'
        cls.selenium = Remote(
            command_executor='http://selenium:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.FIREFOX
        )
        super().setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super().tearDownClass()

    def test_page_loads(self):
        self.selenium.get(self.live_server_url)
        assert "My Site" in self.selenium.title

此版本的测试还引发OSError: [Errno 99] Address not available

这里有人对我可能做错了什么以及如何解决它有任何指示吗?让Selenium与我现有的后端测试一起工作真是太好了。

最佳答案

想想我已经解决了。我没有使用pytest-django live_server固定装置,而是使用现有的LiveServer类添加了自己的装置。

这是我的附加装置:

import pytest
import socket

from pytest_django.live_server_helper import LiveServer

@pytest.fixture(scope='session')
def test_server() -> LiveServer:
    addr = socket.gethostbyname(socket.gethostname())
    server = LiveServer(addr)
    yield server
    server.stop()

您可能还需要将Django容器的ip添加到ALLOWED_HOSTS中。我这样做是这样的:
if env("USE_DOCKER") == "yes":
    import socket

    ALLOWED_HOSTS = [socket.gethostbyname(socket.gethostname())]

关于django - 将Selenium与Pytest结合使用以在Docker中运行的Django 2.2项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176826/

相关文章:

python - 如何在开发人员之间管理 django 1.7 中的迁移?

java - 使用 WebDriver 在基于 CSS 的菜单中选择一个选项

linux - 与 Docker 共享设备(网络摄像头、USB 驱动器等)

testing - 需要一些关于安全测试的信息

docker - 带有php链接的docker-compose.yml nginx

ubuntu - 无法通过 apt-key 添加 docker GPG key

django - 如何检测模板中的表单集是否有任何错误?

python - Django 新的 syncdb super 用户在 pycharm 中崩溃

javascript - Django 和静态网站

java - Selenium:setHeadless() 对 ChromeDriver 没有影响