量角器映射函数返回undefined

量角器映射函数返回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.

例如,页面:

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

Unfortunately, my expects are returning undefined.

我做错了什么?我正在使用Protractor 2.0。

What am I doing wrong? I'm using Protractor 2.0.

这让我很困惑所以我想我会添加一个答案来帮助别人......

This confused me so I thought I'd add an answer to help others...

虽然我明白 map()会返回一个承诺,因为我在期待,我认为它会被解决,然后应该像一个数组。不。它返回一个看起来像数组的对象,但是不是一个数组。

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. Nope. It returns an object, that looks like an array, but is not an array.