protractor icon indicating copy to clipboard operation
protractor copied to clipboard

How to get iterator index/key when looking for elements by repeater?

Open nvitas opened this issue 13 years ago • 5 comments

Is there a way to access the iterator index/key when looking for elements by repeater?

protractor.By.repeater("(id,cat) in pets")

In this case I'm looking to get access to "id" of the cat. The "id" is NOT one of the columns displayed as a value in the table, it is used for navigation as ng-click="goto('/pets/'+cat.id)". There is no binding in the HTML such as {{id}} or {{cat.id}} so doing the following:

ptor.findElements(protractor.By.repeater("(id,cat) in pets").column('cat.id')) returns an empty element: []

I've also, unsuccessfully, tried doing something like:

ptor.findElement(protractor.By.repeater("(id,cat) in pets").row(0).column('cat.id'))

What is the correct way to access that specific index?

nvitas avatar Nov 25 '13 14:11 nvitas

Is protractor.By.repeater("(id,cat) in pets") working for you?

I would try ptor.findElement(protractor.By.repeater("cat in pets").column('id'))

MexicanAce avatar Nov 25 '13 15:11 MexicanAce

When I try "ptor.findElement(protractor.By.repeater("cat in pets").column('id'))" I get the following error:

Error: Unsupported locator strategy: findArrayOverride

To answer your question, 'protractor.By.repeater("(id,cat) in pets")' is working. I can pull out the information from columns used in the table by doing something like 'ptor.findElements(protractor.By.repeater("(id,cat) in pets").column('cat.name'))' since {{cat.name}} is present in the html structure of the table being displayed.

nvitas avatar Nov 25 '13 16:11 nvitas

Currently, the By.repeater locator will find all children html elements that match the identifier and have the ng-binding class.

So if cat.id is not bound in any children nodes of the repeater, it will not be found by the locator.

As an alternative, you could parse through the ng-click attribute to grab the id:

var promisedCats = ptor.findElements(By.repeater("(id,cat) in pets");

promisedCats.then(function(cats){
    for(int i = 0; i < cats.length; i++){
        var catID = cats[0].getAttribute("ng-click").then(value){
            return value.substring(13);
        }
    }
}

Something along these lines might work out for you.

MexicanAce avatar Nov 25 '13 19:11 MexicanAce

Thanks for your quick reply, that is a very clever way of trying to get at it. The problem with that approach for me is that it will return the actual string "ecu.id" when accessing the ng-click attribute as opposing to the resolved id of say '1','5','12'.

var catID = cats[i].getAttribute("ng-click").then(function (value) { return value; })

returns: "goto('/pets/'+cat.id)"

I guess the thing to take away from this is that to get at the resolved cat.id it needs to be explicitly bound in one of the child nodes.

nvitas avatar Nov 25 '13 19:11 nvitas

Looks like this was asked on stackOverflow as well, which is a great place for this kind of question. I answered there: http://stackoverflow.com/questions/20154148/how-to-get-iterator-index-key-using-protractor-angular

juliemr avatar Nov 26 '13 01:11 juliemr