【问题标题】:Django and Selenium Web testing error: [Errno 10054]Django 和 Selenium Web 测试错误:[Errno 10054]
【发布时间】:2012-10-25 22:44:30
【问题描述】:

我正在使用 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)

它运行完成,但转储上述异常。这个想法是测试一个简单的注册页面。单击提交按钮后,页面重新显示并提示用户填写其他表单字段。正如预期的那样,一切似乎都正常工作,但为什么会出现错误?我错过了什么吗?

【问题讨论】:

    标签: django selenium


    【解决方案1】:

    我通过将 URL 中的 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

    【讨论】:

    • 哇,我已经有一个多月没有从事这个项目了,但我一定会尝试一下你的建议。听起来很有道理,谢谢!
    • 从昨天开始我注意到,虽然我的测试用例已经修复,但我仍然可以编写一个产生错误的测试。似乎它还取决于测试结束时您最终到达的页面:stackoverflow.com/a/12306367/214091
    • 对我有用,要注意一件快速的事情:如果 'localhost' 出现在 url 中的任何位置,它将被替换 ('news.com/neighboursdislikelocalhostileman/')。可能值得使用urlparse 或正则表达式来确定。
    【解决方案2】:

    使用 FireFox,我能够通过充分减慢关机过程来解决此问题:

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

    【讨论】:

      【解决方案3】:

      用 127.0.0.1 替换 localhost 对我不起作用,添加 sleep 只会减慢测试速度。我能够通过在退出浏览器之前调用刷新来摆脱错误:

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

      就我而言,是在<link><script> 标签中加载静态文件导致了问题。但是当我在退出前添加refresh 时它就消失了。

      【讨论】:

      • 调用刷新为我做了
      【解决方案4】:

      Django 调试工具栏 会产生大量此类错误。卸载后重试

      【讨论】:

        猜你喜欢
        • 2012-11-08
        • 2018-02-05
        • 1970-01-01
        • 2016-05-30
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 2016-08-05
        相关资源
        最近更新 更多