0

I have an array of strings, which I need to allow for duplicates.

vm.list = ["item","item","item","item","item"]

this is handled in the html with

<ul class="listItems>
    <li ng-repeat="item in ctrl.list track by $index"></li>
</ul>

This displays fine in the DOM, no issues, but I run into problems when I try to protractor test the ng-repeat, since I can't unit test it.

So my test is something like.

Then("List items should contain {int} items.", function(listLength){
    return element(by.css(".listItems").all(by.repeater('item in ctrl.list track by $index')).then(function(list){
        return expect(list.length).equal(listLength);
    });
});

I run my tests and it fails with Expect 0 to be 5 But if I make them all unique it works fine, how can I fix this?

BrokenEyes
  • 192
  • 2
  • 18

1 Answers1

0

I believe this should work for you:

Then("List items should contain {int} items.", function(listLength){
    let list = element(by.css(".listItems").all(by.repeater('item in ctrl.list'));
    expect(list.count()).toBe(listLength);
});

Source: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.count

Edit: Sorry, I misinterpreted your question, it appears track by does not work with duplicate values in an array. See: https://docs.angularjs.org/api/ng/directive/ngRepeat. You can substitute your own tracking function.

Here is an example of a similar issue: https://stackoverflow.com/a/28232640/3111958

2nd Edit: After looking into it some more, protractor does not use the track by portion. I updated the code block to reflect that. Check out this issue: https://stackoverflow.com/a/37535098/3111958

Andrew H
  • 453
  • 1
  • 3
  • 8
  • but 'track by $index' should handle duplicates as given in the documentations example
    {{n}}
    which works when rendered in html but for some reason is failing in Protractor.
    – BrokenEyes Oct 09 '17 at 07:58
  • Actually it looks like you shouldn't be including the `track by $index` in the repeater, please see the edited answer. – Andrew H Oct 10 '17 at 11:39
  • Ya, I thought that too, but nope, that didn't work either :( – BrokenEyes Oct 11 '17 at 08:56
  • Hm, did you try testing it with a hardcoded list? Maybe the list isn't being populated at run time or maybe it isn't loaded in the DOM in time for protractor to inspect it. – Andrew H Oct 11 '17 at 11:26
  • Yes, its an odd one, it works find if you view the html normally, but trying to count the
  • 's in protractor is where it fails, it's like protractor can't render the ng-repeat correctly, again if I give it a list with no duplicates it work fine. I've hit so many brick walls with this tool though, nothing surprises me now, its a bit of a dark art, which sometimes fails for what seems like, no good reason.
  • – BrokenEyes Oct 12 '17 at 08:18