我正在构建一个Django网站,我正在使用Selenium来测试我的网页。我的问题是,当我运行测试时,浏览器启动,但没有加载任何页面,甚至没有尝试加载。它只是打开空白,测试挂起。在我看来,liveserver没有开始。我在Apache2和WSGI上运行,但我的理解是Selenium测试由Django的内置Web服务器运行。知道什么可能是错的吗?相关文件如下:
tests.py:
from selenium.webdriver.firefox.webdriver import WebDriver
class MyProjectLiveServerTestCase(LiveServerTestCase):
@classmethod
def initSeleniumDriver(cls):
cls.driver = WebDriver()
@classmethod
def closeSeleniumDriver(cls):
cls.driver.quit()
def testIndexShouldLoad(self):
self.driver.get('%s%s' % (self.live_server_url, '/nd5/mybook/'))
self.assertEqual(len(self.driver.find_elements(
By.CSS_SELECTOR,
'span#copyright'
)), 1)
settings.py:
# Test database runs on SQLite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.path.realpath(os.path.dirname(__file__)), '..', 'myprojectdb'),
}
}
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
我正在使用django-nose,所以我以这种方式执行测试:
python manage.py test --exe
如果您需要查看代码的其他任何部分,请告诉我。
更新
以下是更新:我发现Firefox没有加载页面的原因是因为我的Firefox版本比Selenium支持的最新版本更新。所以我切换到Chrome,现在请求浏览器中的URL。但是,找不到页面(404错误)。这必须意味着liveserver仍未运行。我的测试在运行时不会打开liveserver。知道为什么吗?端口未被阻止 - 我检查了。
答案 0 :(得分:0)
我想你忘了选择使用webdriver:
class SomethingTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(2)
def tearDown(self):
self.browser.quit()
def test_user_can_log_in(self):
self.browser.get(self.live_server_url + reverse('something'))
self.fail('write rest of the test')
答案 1 :(得分:0)
这可能不是你的问题,但我的意思是LiveServerTestCase
从setUpClass
启动服务器主题,我在没有调用super(MyProjectLiveServerTestCase, self).setUpClass()
的情况下定义了该主题。