2

每当我尝试基于 构造字符串时self.live_server_url,我都会收到 pythonTypeError消息。例如,我尝试了以下字符串结构(下面的表格 1 和 2),但我遇到了相同的TypeError. 我想要的字符串是"/lists"附加的 Live Server URL。注意:实际测试确实成功创建了服务器,我可以手动访问服务器,更具体地说,我可以手动访问我尝试以编程方式构建的确切 URL(例如'http://localhost:8081/lists')。

TypeErrors 出现在这些字符串结构中。

# FORM 1
lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')

# FORM 2
lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')

self.browser.get(lists_live_server_url)

这种形式没有python错误(没有附加到字符串),尽管我的测试失败了(正如我所期望的那样,因为它没有访问/lists)。

self.browser.get(self.live_server_url)

这是我得到的python错误。

/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test functional_tests.lists_tests.LiveNewVisitorTest.test_can_start_a_list_and_retrieve_it_later /Users/myusername/PycharmProjects/mysite_proj
    Testing started at 11:55 AM ...
    Creating test database for alias 'default'...
    Traceback (most recent call last):
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137, in run
        self.result = application(self.environ, self.start_response)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1104, in __call__
        return super(FSFilesHandler, self).__call__(environ, start_response)
      File "/usr/local/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
        response = self.get_response(request)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1087, in get_response
        return self.serve(request)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1099, in serve
        return serve(request, final_rel_path, document_root=self.get_base_dir())
      File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
        fullpath = os.path.join(document_root, newpath)
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/posixpath.py", line 82, in join
        path += b
    TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

我是否在不知不觉中试图修改live_server_url导致这些TypeErrors 的 , ?我怎样才能以编程方式构建一个字符串live_server_url + "/lists"

这是我正在尝试的测试...

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase

class LiveNewVisitorTest(LiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.close()

    def test_can_start_a_list_and_retrieve_it_later(self):
        #self.browser.get('http://localhost:8000/lists')
        #self.browser.get('http://www.google.com')
        #lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
        #lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
        lists_live_server_url = self.live_server_url
        self.browser.get(lists_live_server_url)

        self.assertIn('To-Do', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('To-Do', header_text)
4

2 回答 2

3

请参阅 Reddit 上的此讨论,其中包含相同的错误Traceback

基本上,这不是Selenium测试中的任何问题,而是项目的静态文件配置问题。

根据您的问题,我相信其中的关键线Traceback是:

File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
    fullpath = os.path.join(document_root, newpath) 

此行表示os.path.join在 内尝试不成功django.views.static

STATIC_ROOT在你的项目文件中设置settings.py,你应该很好。

于 2015-09-23T04:44:58.263 回答
1

改用 StaticLiveServerTestCase 可能会有所帮助

于 2016-11-26T04:10:08.633 回答