假设有一个带有多个小部件的应用程序,每个小部件都有自己的标题等内容,我想映射每个小部件的元素,以便在测试中更容易处理。
例如,一个页面:
this.widgets = element.all(by.css('ul.widget-grid')).map(function(widget, index) {
return {
index: index,
title: widget.element(by.css('div.title')).getText()
};
});
然后在我的规格中:
expect(page.widgets[0].index).toBe(0);
expect(page.widgets[0].title).toBe('The Title');
不幸的是,我的期望值返回了undefined
。
我做错了什么?我正在使用Protractor 2.0。
失败:无法读取未定义属性'index'
。 - Brinethis.widgets.then(function (widgets) { console.log(widgets); });
,会打印出什么? - alecxe[]
- 你没有匹配ul.widget-grid
CSS 选择器的元素。 - alecxemap
在2.0中是否有问题。因为当我运行this.widgets.then(function(widgets){console.log(widgets);});
时,我得到了[ ]
;但是当我运行console.log(element.all(by.css('ul.widget-grid')));
时,我得到了[object Object]
...正如预期的那样。 - Brine