stuplum/astrolabe

How to get item by index from findElements?

Opened this issue · 1 comments

header_PersonName: { get: function() { return this.findElements(this.by.binding('{{header.displayName}}')).then(function (elems) {
    return elems[0];
}); } }

My test:
expect(page.header_PersonName).toBeDefined();

When I run protractor I get this error:
Error: expect called with WebElement argment, expected a Promise. Did you mean to use .getText()?

Thanks for any help.

Use a function instead:

header_PersonName: {
    // This is how to find the actual header element.
    get: function() { return this.findElements(this.by.binding('{{header.displayName}}')); }
},

getPersonName: {
    value: function () {
        // This uses the element from above and gets the first element from it.
        return this.header_PersonNames.then(function (elems) {
            return elems[0];
        });
    }
}

Let me know if that works.

Ok, I'm pretty sure that won't work.

I think your problem is that your trying to make a web element selector that uses findElements, and then you're turning around and returning the first element. When you do that, you force the evaluation of the promise inside of astrolabe, and it in turn gives you back a web element.

You need to avoid evaluating the promise here. This should work:

header_PersonName: {
    get: function() { return this.findElement(this.by.binding('{{header.displayName}}')); }
}

// ...

expect(page.header_PersonName).toBeDefined();

By using findElement, you don't get back a list, and you don't need to evaluate it to get the first element from it.