Selenium 和 django 调试工具栏
Selenium and django debug toolbar
我的网站使用 Django 1.11.1 和 Django 调试工具栏 1.8 运行在我的开发服务器上运行良好 (vagrant/gunicorn/nginx)。我的常规测试套件 运行 没问题。但是,当我告诉它 运行 硒测试时,它会以 'djdt' is not a registered namespace error
结束。我在 url、中间件和配置中有适当的条目。如果我卷曲 localhost,它 returns 带有调试工具栏的主页。如果我将工具栏设置为不显示 show_toolbar 代码,我的 selenium 测试仍然会崩溃,但该网站仍然可以正常工作(并且不显示调试工具栏)。
关于如何让我的 selenium 测试达到 运行 的想法?
> python manage.py test ann.selenium
Creating test database for alias 'default'...
Installed 8 object(s) from 1 fixture(s)
Installed 3 object(s) from 1 fixture(s)
Installed 2 object(s) from 1 fixture(s)
Installed 16 object(s) from 7 fixture(s)
Installed 6 object(s) from 1 fixture(s)
Installed 1 object(s) from 1 fixture(s)
Adding auth tokens for the API...
System check identified no issues (0 silenced).
Internal Server Error: /
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/base.py", line 77, in reverse
extra, resolver = resolver.namespace_dict[ns]
KeyError: 'djdt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/utils/deprecation.py", line 142, in __call__
response = self.process_response(request, response)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/middleware.py", line 134, in process_response
bits[-2] += toolbar.render_toolbar()
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/toolbar.py", line 64, in render_toolbar
return render_to_string('debug_toolbar/base.html', context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/defaulttags.py", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/base.py", line 87, in reverse
raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'djdt' is not a registered namespace
显然,如果您正在进行 selenium 测试,答案是不加载 django_debug_toolbar。
import sys
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
if not TESTING:
MIDDLEWARE += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar', )
如果您希望看到 django 调试工具栏。
如果是 django 1.11 ,那么请尝试下面的代码。将 "patterns" 替换为 [](列表)。我在 django-debug-toolbar 1.8 中测试过
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
请查看其他内容,例如
DEBUG = True
MIDDLEWARE_CLASSES = ['debug_toolbar.middleware.DebugToolbarMiddleware',]
INTERNAL_IPS = ['127.0.0.1',](相应地添加 ip)
我的网站使用 Django 1.11.1 和 Django 调试工具栏 1.8 运行在我的开发服务器上运行良好 (vagrant/gunicorn/nginx)。我的常规测试套件 运行 没问题。但是,当我告诉它 运行 硒测试时,它会以 'djdt' is not a registered namespace error
结束。我在 url、中间件和配置中有适当的条目。如果我卷曲 localhost,它 returns 带有调试工具栏的主页。如果我将工具栏设置为不显示 show_toolbar 代码,我的 selenium 测试仍然会崩溃,但该网站仍然可以正常工作(并且不显示调试工具栏)。
关于如何让我的 selenium 测试达到 运行 的想法?
> python manage.py test ann.selenium
Creating test database for alias 'default'...
Installed 8 object(s) from 1 fixture(s)
Installed 3 object(s) from 1 fixture(s)
Installed 2 object(s) from 1 fixture(s)
Installed 16 object(s) from 7 fixture(s)
Installed 6 object(s) from 1 fixture(s)
Installed 1 object(s) from 1 fixture(s)
Adding auth tokens for the API...
System check identified no issues (0 silenced).
Internal Server Error: /
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/base.py", line 77, in reverse
extra, resolver = resolver.namespace_dict[ns]
KeyError: 'djdt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/utils/deprecation.py", line 142, in __call__
response = self.process_response(request, response)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/middleware.py", line 134, in process_response
bits[-2] += toolbar.render_toolbar()
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/toolbar.py", line 64, in render_toolbar
return render_to_string('debug_toolbar/base.html', context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/defaulttags.py", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/base.py", line 87, in reverse
raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'djdt' is not a registered namespace
显然,如果您正在进行 selenium 测试,答案是不加载 django_debug_toolbar。
import sys
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
if not TESTING:
MIDDLEWARE += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar', )
如果您希望看到 django 调试工具栏。 如果是 django 1.11 ,那么请尝试下面的代码。将 "patterns" 替换为 [](列表)。我在 django-debug-toolbar 1.8 中测试过
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
请查看其他内容,例如
DEBUG = True
MIDDLEWARE_CLASSES = ['debug_toolbar.middleware.DebugToolbarMiddleware',]
INTERNAL_IPS = ['127.0.0.1',](相应地添加 ip)