如何使用documet.getElementById和getElementsByClassName为display no
How to do jasmine test case for display none css property using documet.getElementById and getElementsByClassName
我是茉莉花测试用例的新手,我尝试为选择模块做茉莉花测试,在做了这个样式之后,属性得到了未定义的
function Selection() {
}
Selection.prototype.expandFlightDetails = function() {
document.getElementsByClassName("flight-details-container").style.display = 'none';
document.getElementById("expandedFlightDetails").style.display = 'block';
};
Selection.prototype.hideFlightDetails = function() {
document.getElementById("expandedFlightDetails").style.display = 'none';
document.getElementsByClassName("flight-details-container").style.display = 'block';
};
我的测试用例是
describe("selection module", function() {
var selection;
beforeEach(function () {
selection= new Selection();
});
afterEach(function () {
});
it('expand the flight details part ' , function(){
selection.expandFlightDetails();
expect(document.body.getElementsByClassName('flight-details-container')[0].style.display).toEqual('none');
});
xit('hide the flight details part ', function(){
selection.hideFlightDetails();
expect(document.getElementById('expandedFlightDetails').style.display).toEqual('none');
});
});
在完成这项工作后,我正在获取并删除beforeEach 的代码
TypeError:无法读取未定义的属性"style"
如果我做了错误的,请纠正我
您在这段代码中有一些错误。
首先在Selection.prototype.expandFlightDetails
中,确保获得数组的第一个结果(您忘记了[0]
):
document.getElementsByClassName("flight-details-container")[0]
Selection.prototype.hideFlightDetails
的相同注释
然后,在您的测试套件中,您创建了一个名为selection
的Selection实例,但在两个测试中,您都使用了一个称为flightselection
的变量,该变量在任何地方都没有声明。不应该是selection
吗?
最后,您的问题似乎是试图在测试中操纵'flight-details-container'
,尽管这个元素是在afterEach
回调上创建的。afterEach
意味着它将在每次测试后执行,因此在测试期间不存在。
相关文章:
- 将display属性更改为visible flicks,然后再次消失
- Javascript GetElementByID has no value
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- 为什么我可以'不要使用xx[this.index].style.display?它's说“;无法读取属性
- Javascript window.open()toolbar=no不起作用
- Regex表示10位数字,其中没有't允许所有零、所有单个no和序列号
- 如何在使用jQuery应用display:none后正确显示元素
- placeHolder id未使用style.display显示
- `npm install` killed for large package.json, no npm-debug.lo
- 是否可以在网页上用另一种字体设置jqmath-display的样式
- YES或NO表示对象事件中的分号
- Javascript -> CSS (display:none to display:block)
- Wordpress AJAX with raw javascript? NO Jquery
- 当display:none时,getElementById()无法获取元素
- 禁用复选框上的输入框 jquery NO onchange
- no-op if 语句会导致 Google 图表失败
- Greasemonkey:当style=“display: none;”发生变化时如何做某事
- 如何使用documet.getElementById和getElementsByClassName为display no
- 通过将按钮id从Code-behind传递给Jquery Function,Set buttons display no
- Javascript display(); no showing pop, push, unshift, shift?