sukh
sukh

Reputation: 358

Retrieve element within repeater in Protractor

I'm sure this is a simple one but my eyes are slowly dying on me.

Basically, is there a way to get an element within a repeater? For example, I have a bunch of anchors within a repeater, however the class attached to the anchor is generic, so something like:

<li ng-repeat="mainItem in mainNav" class="nav">
    <a class="link" href="#">Link 1</a>
</li>

<li ng-repeat="subItem in subNav" class="nav">
    <a class="link" href="#">Link 2</a>
</li>

Here I only want to get the anchors within the mainNav repeater (more importantly the corresponding hrefs for each anchor).

I was thinking of filter(), but not entirely sure what function I can apply here to retrieve only the required anchors / hrefs (this actually relates to another question which can be found here where I'm mapping the hrefs - Protractor clicking through an array of elements)

This is what I'm thinking...any help would be appreciated:

element
    .all(by.repeater('mainItem in mainNav')).
    .filter(function(items) {
        // TODO: not sure what i can do here, except something like:
        // items.getText().then({})
    })
    .map(function(link) {
        return link.getAttribute('href');
    })
    .then(function(links) {
        for (var i = 0; i < links.length; i++) {
            browser.get(links[i]);
        }
    });

Upvotes: 2

Views: 674

Answers (1)

alecxe
alecxe

Reputation: 473833

You need map() here:

element.all(by.repeater('subItem in subNav')).map(function (elm) {
    return elm.element(by.css("a.link")).getAttribute("href");
}).then(function (links) {
    for (var i = 0; i < links.length; i++) {
        console.log(links[i]);
    }
});

Upvotes: 2

Related Questions