python - Django : Connection refused to live_server_url

标签 python django selenium docker testing

我正在使用 docker、selenium 和 Django。 我刚刚意识到我正在对我的生产数据库进行测试;而我想在 StaticLiveServerTestCase 自生成的数据库上进行测试。

我试着关注 that tutorial

@override_settings(ALLOWED_HOSTS=['*'])
class BaseTestCase(StaticLiveServerTestCase):
    host = '0.0.0.0'

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.host = socket.gethostbyname(socket.gethostname())
        cls.selenium = webdriver.Remote(
            command_executor='http://hub:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME,
        )
        cls.selenium.implicitly_wait(5)

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


class MyTest(BaseTestCase):

    def test_simple(self):
        self.selenium.get(self.live_server_url)

我尝试连接到 chrome-hub 时没有错误,但是当我尝试打印我的 page_source 时,我不在我的 django 应用程序上,而是在 chrome 错误消息上。这是一部分:

<div class="error-code" jscontent="errorCode" jstcache="7">ERR_CONNECTION_REFUSED</div>

我正在使用 docker-compose 1.Selenium.yml:

chrome:
  image: selenium/node-chrome:3.11.0-dysprosium
  volumes:
    - /dev/shm:/dev/shm
  links:
    - hub
  environment:
    HUB_HOST: hub
    HUB_PORT: '4444'

hub:
  image: selenium/hub:3.11.0-dysprosium
  ports:
    - "4444:4444"
  expose:
    - "4444"

app:
  links:
    - hub

我想我在 docker-compose 文件中做错了什么,但我没弄清楚是什么。

提前致谢!

PS : live_server_url = http://localhost:8081

最佳答案

在使用 docker-compose 时,您需要将运行 Django/测试的容器的 container_name 作为宿主,即

host = 'app'

有关更详细的讨论,请参阅此 question

关于python - Django : Connection refused to live_server_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50601185/

相关文章:

python - Numpy - 计算对角线的乘积

python - 在迁移中访问 django-custom-user manager 方法

java - Xpath 选择元素 Selenium

python - 这是 Python 中某种类型的装饰器吗?

python - 在 Python 中使用 for 循环向日期列添加前导零

python - 电报机器人 : Custom Keyboard [Python]

python - 使用参数 'detail' ', '(' {}' 反转 )' and keyword arguments ' 未找到

python - Django 2.0 密码验证

angularjs - 作为 Maven 构建过程的一部分运行 Protractor 测试

selenium - 无法在 firefox 中使用 selenium 3 注入(inject) javascript