2012-11-06 99 views
4

我正在用Selenium網絡驅動程序運行一些基本的功能性Web測試,並且在我的兩個功能性Web測試用例中發現了這個錯誤。這些測試案例全部通過在最後,但我得到這個在控制檯:Django和Selenium Web測試錯誤:[Errno 10054]

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) 

它運行完成,但是轉儲上面的異常。這個想法是測試一個簡單的註冊頁面。點擊提交按鈕後,頁面重新顯示並提示用戶填寫其他表單域。正如所料,一切似乎正常工作,但爲什麼錯誤?我錯過了什麼嗎?

回答

2

我通過在URL與127.0.0.1更換localhost解決了這一問題:

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

+0

哇,我還沒有在這個項目上工作了一個多月,但我肯定會給你一個嘗試的建議。這聽起來似乎合理,謝謝! – Cliff

+0

由於昨天我注意到,儘管我的測試用例已修復,但我仍然可以編寫一個測試來產生錯誤。它似乎也取決於當測試結束時你最終結束了什麼頁面:http://stackoverflow.com/a/12306367/214091 –

+0

對我來說,有一點需要注意:如果'localhost'出現在它會被替換('http://news.com/neighboursdislikelocalhostileman/')。可能值得使用'urlparse'或正則表達式來確保。 – Wil

4

使用Firefox,我能夠通過充分減慢關閉過程來解決這個:

@classmethod 
def tearDownClass(cls): 
    time.sleep(3) 
    cls.selenium.quit() 
    time.sleep(3) 
    super(TestClass, cls).tearDownClass() 
6

替換localhost與127.0.0.1沒有爲我工作,並添加sleep只會降低測試速度。我能得到通過調用刷新退出瀏覽器之前擺脫錯誤的:

from selenium import webdriver 
browser = webdriver.Firefox() 
# do stuff with browser 
browser.refresh() 
browser.quit() 

對我來說,這是靜態文件<link><script>標記,是導致問題的加載。但是,當我在退出前添加refresh時,它就消失了。

+0

調用刷新爲我做了 –

0

Django調試工具欄會產生大量的這些錯誤。卸載它,然後再試一次

相關問題