Sai*_*irl 1 python django selenium
我正在使用Django收到此错误
RuntimeError: Failed to shutdown the live test server in 2 seconds. The server might be stuck or generating a slow response.
Run Code Online (Sandbox Code Playgroud)
我想在我的网站上的菜单上运行一个简单的测试.菜单采用手风琴风格(向下滚动到概述菜单下的示例.手风琴是Demo中的东西,根据您点击它们的方式菜单出现和消失:http://docs.jquery.com/UI/手风琴#概述)
当其中一个菜单打开时,会出现一个用于搜索的文本框.我要做的是按下该按钮,然后输入一些值并单击"确定"以查看搜索结果.
码
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
Run Code Online (Sandbox Code Playgroud)
测试效果很好,但Django给了我错误.如果我把时间缩短太多,那么selenium将无法找到搜索框,它声称
Element is not currently visible and so may not be interacted with
Run Code Online (Sandbox Code Playgroud)
我不知道如何避免实时测试服务器错误.(从https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase获得的Django信息)
附加信息
这是菜单:
第1部分

然后单击"搜索",大约0.5秒后出现以下内容.
第2部分

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