Skip to main content
 首页 » 编程设计

python之RuntimeError : Failed to shutdown the live test server in 2 seconds. 服务器可能卡住或生成响应缓慢。之Django

2025年05月04日38bhlsheji

我在使用 Django 时遇到这个错误

RuntimeError: Failed to shutdown the live test server in 2 seconds. The server might be stuck or generating a slow response. 

我正尝试在我网站的菜单上运行一个简单的测试。菜单采用 Accordion 样式(向下滚动到概览菜单下的示例。 Accordion 是演示中的东西,菜单根据您单击它们的方式出现和消失:http://docs.jquery.com/UI/Accordion#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 

我不知道如何避免实时测试服务器错误。 (Django资料取自https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase)

附加信息

这是菜单:

第 1 部分

然后单击“搜索”,大约 0.5 秒后出现以下内容。

第 2 部分

请您参考如下方法:

您看到的错误与 SeleniumTests.tearDownClass(cls) 中的清理代码相关。在该方法中,如果您在调用 super(SeleniumTests, cls).tearDownClass() 之前调用 cls.driver.quit(),错误将会消失。