使用 Selenium 的 StaticLiveServerTestCase:ERR_CONNECTION_REFUSED

发布于 2025-01-15 10:30:41 字数 883 浏览 2 评论 0原文

我正在运行 docs< 中的默认代码段/a> 用于使用 selenium 进行测试,但正在使用 Chrome 驱动程序。当我使用 python manage.py test 运行测试时,它无法连接到服务器,似乎无法启动,并抛出错误 ::ERR_CONNECTION_REFUSED。有什么想法吗?

这是片段:

from django.test import LiveServerTestCase
from selenium import webdriver

class MySeleniumTests(LiveServerTestCase):
    # fixtures = ['user-data.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.selenium = webdriver.Chrome(executable_path='./chromedriver')
        cls.selenium.implicitly_wait(10)

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

    def test_login(self):
        self.selenium.get('http://localhost:8000/accounts/login')

I'm running the default snippet from the docs for testing with selenium but 'm using Chrome driver. When I run the tests using python manage.py test it can't connect to the server seems it won't start, throwing the error ::ERR_CONNECTION_REFUSED. Any ideas?

Here is the snippet:

from django.test import LiveServerTestCase
from selenium import webdriver

class MySeleniumTests(LiveServerTestCase):
    # fixtures = ['user-data.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.selenium = webdriver.Chrome(executable_path='./chromedriver')
        cls.selenium.implicitly_wait(10)

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

    def test_login(self):
        self.selenium.get('http://localhost:8000/accounts/login')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2025-01-22 10:30:41

为什么 test_login 中的 url 是硬编码的(localhost:8000)?由于您使用的是 LiveServerTestCase,因此您可以使用 self.live_server_url 访问服务器基本 URL。您在描述中添加的文档链接中也提到了这一点。连接拒绝错误很可能是由于以下原因之一的硬编码 URL 导致的:

  1. 8000 端口上没有活动的服务器或服务
  2. 允许的源设置不允许连接,尽管这完全取决于设置。

Why is the url hard coded(localhost:8000) in test_login? Since you are using LiveServerTestCase, you would be able to access the server base URL with self.live_server_url. This is also mentioned on the docs link you have added in the description. The connection refused error is most likely happening due to the hard-coded URL for one of the following reasons:

  1. No server or service is active at 8000 port
  2. Allowed origin setting is not allowing the connection, though this is entirely dependent on the settings.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文