尚未使用Capybara、PhantomJS测试创建DOM对象(TypeError:undefined)
DOM object not yet created (TypeError: undefined) with Capybara, PhantomJS test
我写了一些javascript代码,当点击按钮时,它会执行以下代码(这只是方法的一部分,这行之前和之后都有一些代码。video_id设置在这行上方的某个地方)。
var data_url = $('.BrightcoveExperience').attr('data').replace(/(videoPlayer=)[^'&]+/, '$1' + video_id);
当我在浏览器中测试它时,代码就可以工作了。没有控制台错误。
然而,我的自动黄瓜测试(Capybara with PhantomJS)失败了,并显示以下错误消息
One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
TypeError: 'undefined' is not an object (evaluating '$('.BrightcoveExperience').attr('data').replace')
基本上$('.BrightcoveExperience').attr('data')
在我的自动测试中返回undefined
出于某种原因。
如果我将代码重写为以下内容,测试就会通过
var brightcove_url = $('.BrightcoveExperience').attr('data')
if (typeof brightcove_url !== 'undefined') {
brightcove_url = brightcove_url.replace(/(videoPlayer=)[^'&]+/, '$1' + video_id);
}
我相信这是由种族状况引起的。不知怎的,在自动测试中,当我点击按钮时,在点击事件代码中,DOM对象$('.BrightcoveExperience')
还没有创建。因此,应该引入某种延迟或等待。
但是怎么做呢?
感谢
尝试检查元素的length
,如
if($('.BrightcoveExperience').length){
var brightcove_url = $('.BrightcoveExperience').attr('data')
// you code here
}
相关文章:
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- 向Angular作用域对象添加对象数组——TypeError
- TypeError:undefined不是对象(正在评估'newWindow.focus')
- 未能运行构造函数:TypeError:对象#<对象>没有方法'addPlugin'
- 破坏相位器.通过按钮点击图像对象会导致TypeError
- 当我将参数设置为对象时,JavaScript返回TypeError
- cordova 2.0.0 给出了 Uncatch TypeError:对象 0 没有方法 'func'
- Angularjs$http删除$q promise导致TypeError:对象不是函数
- Grunt,TypeError:对象没有方法'拆分'
- 谷歌应用程序脚本-正在尝试使用getUrl.TypeError:在对象工作表中找不到函数getUrl
- Karma+PhantomJS TypeError:undefined不是对象(正在评估scope.jjackpot)
- 得到"TypeError:对象不是函数“”;在forEach循环中使用超级测试/超级代理时
- html5拖动&drop-TypeError:Node.appendChild的参数1不是对象
- 未捕获的typeerror对象没有方法'insertafter'
- TypeError:对象不是函数Node.js
- IE8 TypeError对象没有'不支持方法的此属性
- TypeError:undefined不是使用d3.map的对象
- TypeError:undefined不是对象(正在评估'this.props')-React Nati
- 角度嵌套对象-TypeError:无法读取属性'应用程序'的未定义
- 尚未使用Capybara、PhantomJS测试创建DOM对象(TypeError:undefined)