Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get item by index from findElements? #16

Open
RefuX opened this issue Mar 4, 2014 · 1 comment
Open

How to get item by index from findElements? #16

RefuX opened this issue Mar 4, 2014 · 1 comment

Comments

@RefuX
Copy link

RefuX commented Mar 4, 2014

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.

@Droogans
Copy link
Contributor

Droogans commented Mar 5, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants