我正在使用Selenium Web驱动程序运行一些基本的功能性Web测试,我在两个功能性Web测试用例中注意到了这个错误。测试用例都在最后通过,但我在控制台中得到了这个:
Exception happened during processing of request from ('127.0.0.1', 1169)
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Traceback (most recent call last):
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\test\testcases.py", line 981, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python27\Lib\SocketServer.py", line 638, in __init__
self.handle()
File "C:\Python27\Lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\Lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Traceback (most recent call last):
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\test\testcases.py", line 981, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python27\Lib\SocketServer.py", line 638, in __init__
self.handle()
File "C:\Python27\Lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\Lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 1170)
----------------------------------------
Destroying test database for alias 'default'...
以下是其中一个测试用例的示例:
def test_can_join_main_site(self):
self.browser.get(self.live_server_url)
self.browser.find_element_by_link_text('Register').click()
time.sleep(5)
self.browser.find_element_by_name('submit').click()
time.sleep(5)
它运行完成但转储上述异常。我们的想法是测试一个简单的注册页面。单击提交按钮后,页面将重新显示并提示用户填写其他表单字段。正如所料,一切似乎都正常,但为什么错误?我错过了什么吗?
答案 0 :(得分:6)
用127.0.0.1替换localhost
对我来说不起作用,添加sleep
只会减慢测试速度。我在退出浏览器之前通过调用刷新来摆脱错误:
from selenium import webdriver
browser = webdriver.Firefox()
# do stuff with browser
browser.refresh()
browser.quit()
在我的情况下,加载<link>
和<script>
标记中的静态文件导致了问题。但是当我在退出之前添加refresh
时它就消失了。
答案 1 :(得分:4)
使用FireFox,我能够通过充分减慢关机过程来解决这个问题:
@classmethod
def tearDownClass(cls):
time.sleep(3)
cls.selenium.quit()
time.sleep(3)
super(TestClass, cls).tearDownClass()
答案 2 :(得分:2)
我通过在网址中将localhost
替换为127.0.0.1
来解决问题:
url = self.live_server_url
url = url.replace('localhost', '127.0.0.1')
self.driver.get('%s%s' % (url, reverse('whatever')))
我在这里找到了解决方案:https://code.djangoproject.com/ticket/15178
答案 3 :(得分:0)
Django调试工具栏会产生大量的错误。卸载它然后重试