当我 运行 我的测试时,Django LiveServerTestCase 挂起
Django LiveServerTestCase hangs when I run my tests
我正在尝试使用 Django 1.10.4 设置 LiveServerTestCase。每当我 运行 我的测试时,浏览器打开时挂起并且无法访问本地主机。我的前端是一个单独的 angular/react 应用程序。因此,我使用 g运行t build 构建我的静态资产,然后使用 运行 collectstatic。下面是我的测试代码。
from django.test.testcases import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class ChromeTestCase(LiveServerTestCase):
@classmethod
def setUpClass(cls):
super(ChromeTestCase, cls).setUpClass()
cls.driver = webdriver.Chrome('/path/to/chromedriver')
cls.driver.implicitly_wait(10)
cls.wait = WebDriverWait(cls.driver, 10)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
super(ChromeTestCase, cls).tearDownClass()
def test_user_sign_up_from_form(self):
self.driver.get('%s%s' % (self.live_server_url, '/'))
self.wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="email"]')))
email_input = self.driver.find_element_by_xpath(
'//input[@id="email"]')
email_input.send_keys("test@gmail.com")
password_input = self.driver.find_element_by_xpath(
'//input[@id="password"]')
password_input.send_keys("secret")
signup_button = self.driver.find_elements_by_xpath(
'//*[@id="signup_button"]')
signup_button.click()
url = self.live_server_url + '/home'
self.assertEquals(self.driver.current_url, url)
有谁知道为什么我的测试无法到达测试服务器?
此外,url 我的测试服务器创建的是 https。
该问题最终与在生产环境中将请求重定向到 https 的中间件有关。我通过删除该中间件使我的测试正常工作。
您可以使用 LiveServerTestCase
启用 DEBUG
,添加以下行:
from django.test import override_settings
@override_settings(DEBUG=True)
class ChromeTestCase(LiveServerTestCase):
我正在尝试使用 Django 1.10.4 设置 LiveServerTestCase。每当我 运行 我的测试时,浏览器打开时挂起并且无法访问本地主机。我的前端是一个单独的 angular/react 应用程序。因此,我使用 g运行t build 构建我的静态资产,然后使用 运行 collectstatic。下面是我的测试代码。
from django.test.testcases import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class ChromeTestCase(LiveServerTestCase):
@classmethod
def setUpClass(cls):
super(ChromeTestCase, cls).setUpClass()
cls.driver = webdriver.Chrome('/path/to/chromedriver')
cls.driver.implicitly_wait(10)
cls.wait = WebDriverWait(cls.driver, 10)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
super(ChromeTestCase, cls).tearDownClass()
def test_user_sign_up_from_form(self):
self.driver.get('%s%s' % (self.live_server_url, '/'))
self.wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="email"]')))
email_input = self.driver.find_element_by_xpath(
'//input[@id="email"]')
email_input.send_keys("test@gmail.com")
password_input = self.driver.find_element_by_xpath(
'//input[@id="password"]')
password_input.send_keys("secret")
signup_button = self.driver.find_elements_by_xpath(
'//*[@id="signup_button"]')
signup_button.click()
url = self.live_server_url + '/home'
self.assertEquals(self.driver.current_url, url)
有谁知道为什么我的测试无法到达测试服务器?
此外,url 我的测试服务器创建的是 https。
该问题最终与在生产环境中将请求重定向到 https 的中间件有关。我通过删除该中间件使我的测试正常工作。
您可以使用 LiveServerTestCase
启用 DEBUG
,添加以下行:
from django.test import override_settings
@override_settings(DEBUG=True)
class ChromeTestCase(LiveServerTestCase):