使用 selenium 运行 django-test 时出现断管错误

Posted

技术标签:

【中文标题】使用 selenium 运行 django-test 时出现断管错误【英文标题】:Broken Pipe Error while running django-test with selenium 【发布时间】:2012-12-07 21:15:47 【问题描述】:

在使用 selenium(没有远程,没有 xvfb)运行 django 测试时,我总是遇到以下异常:

Creating test database for alias 'default'...

Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/usr/lib/python2.7/wsgiref/handlers.py", line 210, in write
self.send_headers()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "/usr/lib/python2.7/socket.py", line 324, in write
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 44089)
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py", line 981, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 139, in __init__
----------------------------------------
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

Destroying test database for alias 'default'...

Process finished with exit code 0

使用 django 1.4 和 selenium python-bindings 2.28.0 和 Firefox WebDriver 使用 LiveServerTestCase 运行测试。有人知道如何解决吗?

【问题讨论】:

Broken pipe error selenium webdriver, when there is a gap between commands?的可能重复 【参考方案1】:

确保请求页面的浏览器正在等待响应。

如果我没记错的话,有 selenium_client.implicitly_wait(sec)selenium_client.set_page_load_timeout(sec) 命令,请确保您正在使用它。

如果不是,服务器将尝试写入已损坏的管道,因为浏览器在发送响应之前关闭了连接。

【讨论】:

谢谢!虽然 selenium_client.waitForPageToLoad(ms) 在那个版本/python-bindings 中不存在,但诀窍是设置隐式超时:selenium_client.implicitly_wait(sec)selenium_client.set_page_load_timeout(sec) 但事实并非如此。 selenium_client.waitForPageToLoad(ms) 不存在。

以上是关于使用 selenium 运行 django-test 时出现断管错误的主要内容,如果未能解决你的问题,请参考以下文章

django-tables2 每行添加按钮

如何更改 view.py 中 django-tables2 表的列标题?

css django-tables2(https://github.com/bradleyayers/django-tables2)的bootstrap模板,目前依赖于django-bootstrap

仅使用 chrome 控制台运行 selenium 命令

使用 Headless Chrome Webdriver 运行 Selenium

在 docker 中使用 selenium 运行 django 测试