我收到 Django 的这个错误
RuntimeError: Failed to shutdown the live test server in 2 seconds. The server might be stuck or generating a slow response.
我正在尝试在我网站上的菜单上运行一个简单的测试。菜单采用手风琴风格(向下滚动到概览菜单下的示例。手风琴是演示中的东西,菜单根据您单击它们的方式出现和消失:http: //docs.jquery.com/UI/手风琴#overview )
当其中一个菜单打开时,会出现一个用于搜索的文本框。我要做的是按下该按钮,然后输入一些值并单击“Enter”以查看搜索结果。
代码
from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time
class SeleniumTests(LiveServerTestCase):
fixtures = ['testData.json',]
@classmethod
def setUpClass(cls):
cls.driver = WebDriver()
super(SeleniumTests, cls).setUpClass()
@classmethod
def tearDownClass(cls):
super(SeleniumTests, cls).tearDownClass()
cls.driver.quit()
def test_example(self):
#load the site
self.driver.get('%s%s' % (self.live_server_url, '/testingDB/'))
#find the accordion button
elem1 = self.driver.find_element_by_id("search_button")
#click it
elem1.click()
#wait a little bit so the accordion loads
self.driver.implicitly_wait(5)
#could've also used this command ->time.sleep(1)
#find the element that now appeared
elem = self.driver.find_element_by_name("q")
#enter the search query and press enter
elem.send_keys("selenium" + Keys.RETURN)
#assert that a new page loaded, with Search in the title
assert "Search" in self.driver.title
测试效果很好,但 Django 给了我错误。如果我将时间缩短太多,那么 selenium 将无法找到搜索框,它声称
Element is not currently visible and so may not be interacted with
我不知道如何避免实时测试服务器错误。(从https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase获得的 Django 信息)
附加信息
这是菜单:
第1部分
然后单击“搜索”,大约 0.5 秒后出现以下内容。
第2部分