Skip to main content
 首页 » 编程设计

python之我们如何使用带有 Selenium 的 Django LiveServerTestCase 来测试 https url

2024年11月24日32softidea

我们的端到端测试使用 Django 的 LiveServerTestCase 和 Selenium。到目前为止,我们只能在不安全的 URL 上运行这些。然而,我们使用的一些库(Square)要求页面在 https 上,即使在沙盒模式下也是如此。

当尝试连接到安全 url 时,Selenium/Chrome Webdriver 仅显示不支持标准 SSL 错误:

This site can’t provide a secure connection chezpierre.localtest.me sent an invalid response. 
ERR_SSL_PROTOCOL_ERROR 

有谁知道是否可以在 LiveServerTestCase 上启用 https?

如果没有,是否有人对此有解决方法?我试图避免在我们的构建盒上运行单独的 https 代理,但它似乎是唯一的方法。

请您参考如下方法:

经过快速研究,我发现这在旧代码票据建议的 Django 中是不可能的 https://code.djangoproject.com/ticket/25328

我还发现您可以设置一个隧道来绕过这个问题。然而,这适用于 Django 开发服务器。这有点棘手,所以我留下了指向帖子的链接,因为该方法相当长:

https://www.ianlewis.org/en/testing-https-djangos-development-server

或

How can I test https connections with Django as easily as I can non-https connections using 'runserver'?

备选方案 - 在我看来更好

还有一种使用外部包的更简单的方法。它为您提供开箱即用的支持 https 的 django 开发服务器。项目活跃并维护

https://github.com/teddziuba/django-sslserver