hello123
hello123

Reputation: 951

Empty array returned from ng-repeat in protractor test

I have the following html

<div data-ng-controller="exampleCtrl as ec">
   <div data-ng-repeat="item in ec.items">
      <blockquote>
         {{item.name}}
      </blockquote>
   </div>
</div>

In my example.page.js file I attempt to get the list of item.name using

this.names= element.all(by.repeater('item in ec.items').column('item.name')).map(function (names) {
   return names.getText();
});

Then in my example.step.js file I do

examplePage.names.then(function (names) {
   console.log("Names length : " + names.length);
};

(Here examplePage is a new example.page.js)

Names has length 0 according to the log, though on screen there are 2 items visible each with names.

Also, if I just do this.names = element.all(by.repeater('item in ec.items').column('item.name')), then I can see that names has length 2, but don't know how to get hold of the 'names' text. When I log them I just get [object Object], [object Object].

So it seems that calling the map function is somehow losing the 2 items.

Using the evaluate function as suggested I get items.length = 1, then if i do console.log(items[0]) I get the whole element printed like

ElementFinder { ptor_: Protractor { ......

I've tried doing getText() of this element, but get the same thing printed out.

Any suggestions why this might be? I have done very similar things recently and it has worked, but for some reason on this occasion it's not working.

Thanks

Upvotes: 0

Views: 283

Answers (2)

AdityaReddy
AdityaReddy

Reputation: 3645

Can you try evaluate() - It should evaluate the scope variables if getText() couldn't get the text

Documentation on evaluate

this.names= element.all(by.repeater('item in ec.items')).map(function (elm) {
    return elm.evaluate('item.name');
});

names.then(function(allValues){
    console.log(allValues.length)
})

Upvotes: 1

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

In addition to Adityas's answer, Instead of using map() function, you can directly use evaluate().

this.names= element.all(by.repeater('item in ec.items').evaluate('item.name').then(function(itemNameAsArray){
 console.log(itemNameAsArray.length)
})

Upvotes: 1

Related Questions