[英]Protractor map function returning undefined
Given an app with multiple widgets on it, each with their own title and whatnot, I would like to map each widget's elements, to make them easy to handle in tests. 给定一个带有多个小部件的应用程序,每个小部件都有自己的标题和诸如此类的东西,我想映射每个小部件的元素,以便在测试中轻松处理。
For example, a page: 例如,一个页面:
this.widgets = element.all(by.css('ul.widget-grid')).map(function(widget, index) {
return {
index: index,
title: widget.element(by.css('div.title')).getText()
};
});
And then in my spec: 然后在我的规范中:
expect(page.widgets[0].index).toBe(0);
expect(page.widgets[0].title).toBe('The Title');
Unfortunately, my expects are returning undefined
. 不幸的是,我的期望是返回
undefined
。
What am I doing wrong? 我究竟做错了什么? I'm using Protractor 2.0.
我正在使用Protractor 2.0。
This confused me so I thought I'd add an answer to help others... 这让我很困惑,所以我想我会添加一个答案来帮助别人......
While I understood that map()
returns a promise, because I was using it in an expect
, I thought it would be resolved, and then should act like an array. 虽然我理解
map()
返回一个promise,因为我在expect
使用它,我认为它会被解析,然后应该像一个数组。 Nope. 不。 It returns an object, that looks like an array, but is not an array.
它返回一个看起来像数组但不是数组的对象。
I ran into this problem, and none of these solved this for me. 我遇到了这个问题,但这些都没有解决这个问题。 For anyone googling like I was here is the real answer:
对于像我在这里谷歌搜索的任何人都是真正的答案:
element.all(by.css('ul.widget-grid')).map(function(widget, index) {
return {
index: index,
title: widget.element(by.css('div.title')).getText()
}
}).then(function(map){
this.widgets = map;
});
Protractor's ElementArrayFinder.prototype.map gave me some hard time. 量角器的ElementArrayFinder.prototype.map给了我一些困难。
The solution that worked for me was a good old for
loop: 为我工作的解决方案是一个很好的旧
for
循环:
var myElements = element.all(by.css('ul li')) - 1;
var n = myElements.length - 1;
for (var i = 0; i < n; i++) {
var elem = myElements.get(i);
var open = elem.element(by.css('.some-class'));
open.click();
var childElem = filter.element(by.css('[some-attribute]'));
expect(childElem.isPresent()).toBe(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.