1

我正在使用 django 1.3 和 Django liveserver 向后移植到 1.3。当我从Django LiverserverTestCase 文档做教程 但导入 django 1.3 时,它工作得很好。它测试“/admin/”页面并且没问题,但是当我尝试测试我的网址时,即使只是“/”,它也会再次进入管理员!为什么?我在谷歌没有找到任何线索,也许这个问题只有一个。

也许我取消了服务器的一些设置。但是 localhost:8000/test 在 runserver 命令后工作正常。

回答是否有人遇到此类问题。

from django_liveserver.testcases import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver

class MySeleniumTests(LiveServerTestCase):
fixtures = ['nstein/test-users.json']

@classmethod
def setUpClass(cls):
    cls.selenium = WebDriver()
    cls.selenium.implicitly_wait(3)
    super(MySeleniumTests, cls).setUpClass()

@classmethod
def tearDownClass(cls):
    super(MySeleniumTests, cls).tearDownClass()
    cls.selenium.quit()

def test_login(self):

    self.selenium.get('%s%s' % (self.live_server_url. '/admin/'))
    body = self.selenium.find_element_by_tag_name('body')
    self.assertIn('Username', body.text)
    username_input = self.selenium.find_element_by_id("id_username")
    password_input = self.selenium.find_element_by_id("id_password")
    password_input.send_keys('123')
    username_input.send_keys('admin')
    password_input.clear()
    self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
    body = self.selenium.find_element_by_tag_name('body')
    self.assertIn('Site administration', body.text) 
    # url is in the urls.py of the app, 
    #selenium gets response 302 but redirects to /admin/
    self.selenium.get('%s%s' % (self.live_server_url. '/test/')) 

应用程序的 urls.py:

#from cms import sitemaps
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from hitcount.views import update_hit_count_ajax
from django.views.generic.simple import direct_to_template

import dselector
from wcms.furniture_today.views import *

parser = dselector.Parser()

urlpatterns = patterns('',
parser.url(r'^test/$', direct_to_template, {'template': 'index.html'}, 'index'),
)

全球网址:

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from wcms.business.views import wcms_admin_redirect

urlpatterns = patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^', include('wcms.admin_urls')),
url(r'^', wcms_admin_redirect),
url(r'^', include('cms.urls')),
)

if settings.DEBUG: # assuming dev server
urlpatterns += patterns('', (r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')))

urlpatterns += staticfiles_urlpatterns()
4

1 回答 1

1

尝试self.selenium.get('%s%s' % (self.live_server_url. '/test/'))添加一个新功能

def test_two(self):
    self.selenium.get('%s%s' % (self.live_server_url. '/test/')) 
于 2012-09-04T17:36:33.823 回答