从 Django 运行 Selenium 时出现超时错误
我在从 Django 运行 SeleniumRC 时遇到问题。我可以运行 Selenium 文档 和 python selenium 客户端文档 在 Python shell 中运行就可以了,无需运行 Django(所以没有 manage.py),但是当我真正尝试时从 django TestCase 或 Django shell 运行 Selenium 时,出现超时错误。 下面是我尝试运行的代码:
from selenium import selenium
from django.test import TestCase
class TestSelenium(TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://127.0.0.1:8000/")
self.selenium.start()
def test_foo(self):
sel = self.selenium
sel.open("/")
使用 manage.py 测试注册运行。TestSelenium 会产生以下错误:
======================================================================
ERROR: test_testformmaintainsdata (registration.tests.TestSelenium)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sam/Documents/dev/app/CustomMade/registration/tests.py", line 158, in setUp
self.selenium.start()
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 189, in start
result = self.get_string("getNewBrowserSession", start_args)
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 223, in get_string
result = self.do_command(verb, args)
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 214, in do_command
response = conn.getresponse()
File "/usr/lib/python2.6/httplib.py", line 990, in getresponse
response.begin()
File "/usr/lib/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.6/socket.py", line 427, in readline
data = recv(1)
timeout: timed out
----------------------------------------------------------------------
Ran 1 test in 12.475s
FAILED (errors=1)
Destroying test database 'default'...
奇怪的是,即使抛出错误并且 Python 停止,SeleniumRC 服务器实际上也会启动 Firefox,但是那么由于 Django 已停止,我无法运行进一步的 Selenium 命令。这是 SeleniumServer 的输出:
14:21:48.362 INFO - Checking Resource aliases
14:21:48.369 INFO - Command request: getNewBrowserSession[*firefox, http://127.0.0.1:8000/, ] on session null
14:21:48.372 INFO - creating new remote session
14:21:48.443 INFO - Allocated session a3ea05a3d0eb4956ba69a67583ea49ba for http://127.0.0.1:8000/, launching...
14:21:48.533 INFO - Preparing Firefox profile...
14:21:51.473 INFO - Launching Firefox...
14:21:55.904 INFO - Got result: OK,a3ea05a3d0eb4956ba69a67583ea49ba on session a3ea05a3d0eb4956ba69a67583ea49ba
有人有什么想法吗?
I'm having trouble running SeleniumRC from Django. I can run the sample code provided by the Selenium docs and the python selenium client docs just fine from in a Python shell without running Django (so no manage.py), but when I actually try to run Selenium from a django TestCase or from the Django shell, I get a timeout error.
Here is the code I'm trying to run:
from selenium import selenium
from django.test import TestCase
class TestSelenium(TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://127.0.0.1:8000/")
self.selenium.start()
def test_foo(self):
sel = self.selenium
sel.open("/")
Running with manage.py test registration.TestSelenium produces the following error:
======================================================================
ERROR: test_testformmaintainsdata (registration.tests.TestSelenium)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sam/Documents/dev/app/CustomMade/registration/tests.py", line 158, in setUp
self.selenium.start()
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 189, in start
result = self.get_string("getNewBrowserSession", start_args)
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 223, in get_string
result = self.do_command(verb, args)
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 214, in do_command
response = conn.getresponse()
File "/usr/lib/python2.6/httplib.py", line 990, in getresponse
response.begin()
File "/usr/lib/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.6/socket.py", line 427, in readline
data = recv(1)
timeout: timed out
----------------------------------------------------------------------
Ran 1 test in 12.475s
FAILED (errors=1)
Destroying test database 'default'...
The strange thing is that even though the error is thrown, and Python stops, the SeleniumRC server does actually launch Firefox, but then I can't run further Selenium commands since Django has stopped. This is the output from SeleniumServer:
14:21:48.362 INFO - Checking Resource aliases
14:21:48.369 INFO - Command request: getNewBrowserSession[*firefox, http://127.0.0.1:8000/, ] on session null
14:21:48.372 INFO - creating new remote session
14:21:48.443 INFO - Allocated session a3ea05a3d0eb4956ba69a67583ea49ba for http://127.0.0.1:8000/, launching...
14:21:48.533 INFO - Preparing Firefox profile...
14:21:51.473 INFO - Launching Firefox...
14:21:55.904 INFO - Got result: OK,a3ea05a3d0eb4956ba69a67583ea49ba on session a3ea05a3d0eb4956ba69a67583ea49ba
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人遇到这个问题,我可以通过在 setUp 方法中增加套接字超时来解决它。
In case anyone else has this problem, I was able to solve it by increasing my socket timeout in the setUp method.
这将以浮点数秒为单位的超时值来解决这个问题:
请参阅 python stdlib 文档
This will do the trick with the timeout value in float seconds:
Refer to python stdlib docs