添加 django-bower 后,Django 测试在 CI 上失败

Django tests failing on CI after adding django-bower

我有一个 Django 项目,其中有一些前端依赖项,因此我尝试添加 django-bower 来帮助我管理它们。

我已经用 bower 版本替换了以前的依赖项,一切似乎都运行良好。我正在 运行 使用 Selenium 进行测试,当我在本地 运行 它们时,它们通过了。

但是,当我继续在 CI 服务器上进行测试时,Selenium 测试失败,并显示一条错误消息,指出它无法在页面上找到元素。这是一个表单元素,我确定它在那里。

我试图找出为什么测试在本地通过但在 CI 服务器上失败。在添加 django-bower 之前,所有测试也在 CI 服务器中通过。

这是 Django 设置中相关部分的片段

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    # Compressor finder
    "compressor.finders.CompressorFinder",
    # Django bower finder
    "djangobower.finders.BowerFinder",
    )

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')

MEDIA_URL = '/uploads/'

BOWER_COMPONENTS_ROOT = os.path.join(BASE_DIR, 'static', 'js')

BOWER_INSTALLED_APPS = (
    'fontawesome#4.3.0',
    'jquery-validation#1.13.1',
    'magnific-popup#1.0.0',
    'masonry#3.2.2',
    'materialize#0.95.3',
)

Bower 组件安装在 static/js/bower_components

CI 服务器上提供的错误消息是

NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'message-form'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"93","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60555","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"bb17c920-c5dd-11e4-9c9b-e7fbd91dc2da\", \"value\": \"message-form\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/bb17c920-c5dd-11e4-9c9b-e7fbd91dc2da/element"}}

任何帮助将不胜感激。我用 Google 搜索了我能想到的所有内容,但似乎找不到解决方案。

这仍然是一个猜测,但我已经看到通过调整测试和添加 Explicit Wait 解决了这类问题。而不仅仅是:

form = driver.find_element_by_id('message-form')

使用:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

form = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "message-form")))

基本上,您等待元素出现在页面上 10 秒,每 500 毫秒检查一次。如果该元素不会在 10 秒内出现,它将抛出 TimeoutException.

我在 Django 项目中使用 Bower,但我没有使用 django-bower。我独立使用它(通过 bower install),让它在 static/bower-components 中存储前端部门,并使用 {% static %} 模板标签在模板中引用该路径。不知道这样做是否会影响您的奇怪测试结果,但值得一试。