8

I'm running some basic functional web test with the Selenium web driver and I'm noticing this error in two of my functional web test cases. The test cases all pass at the end but I get this in the console:

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'...

Here's an example of one of the test cases:

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)

It runs to completion but dumps the above exception. The idea is to test a simple registration page. After clicking the submit button the page re-displays and prompts the user to fill out additional form fields. As expected and everything appears to work correctly but why the errors? Am I missing something?

Cliff
  • 10,586
  • 7
  • 61
  • 102

4 Answers4

8

Replacing localhost with 127.0.0.1 did not work for me, and adding sleep just slows the test down. I was able to get rid of the error by calling refresh before quitting the browser:

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

In my case it was the loading of static files in <link> and <script> tags that was causing the problem. But it went away when I added refresh before quitting.

alan
  • 4,752
  • 21
  • 30
5

Using FireFox, I was able to resolve this by sufficiently slowing the shutdown process:

@classmethod
def tearDownClass(cls):
    time.sleep(3)
    cls.selenium.quit()
    time.sleep(3)
    super(TestClass, cls).tearDownClass()
amoebob
  • 301
  • 4
  • 7
4

I fixed the problem by replacing localhost with 127.0.0.1 in the URL:

    url = self.live_server_url
    url = url.replace('localhost', '127.0.0.1')
    self.driver.get('%s%s' % (url, reverse('whatever')))

I found the solution here: https://code.djangoproject.com/ticket/15178

Dmitry Chornyi
  • 1,821
  • 4
  • 25
  • 33
  • Wow, I haven't worked on this project in over a month but I'll definitely give your suggestion a try. It sounds plausible, thanks! – Cliff Jan 24 '13 at 18:52
  • Since yesterday I noticed, that while my test case was fixed, I could still write a test that produced the error. It seems that it also depends on what page you end up when then test is over: http://stackoverflow.com/a/12306367/214091 – Dmitry Chornyi Jan 24 '13 at 21:00
  • Works for me, one quick thing to note: if 'localhost' appears anywhere in the url it'll be replaced ('http://news.com/neighboursdislikelocalhostileman/'). Might be worth using `urlparse` or a regex to be sure. – Wil Aug 12 '13 at 14:27
1

Django Debug Toolbar generates tons of these errors. uninstall it and try again