使用 Selenium 的 StaticLiveServerTestCase: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么 test_login 中的 url 是硬编码的(localhost:8000)?由于您使用的是 LiveServerTestCase,因此您可以使用
self.live_server_url
访问服务器基本 URL。您在描述中添加的文档链接中也提到了这一点。连接拒绝错误很可能是由于以下原因之一的硬编码 URL 导致的: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: