goldjb goldjb - 3 months ago 17
AngularJS Question

Filtering multiple variables from a repeater using evaluate

I need to filter a repeater with multiple variables with a (type, name) or any other combination of my ngrepeat.

One Variable

I have successfully done this using one variable with this:

element.all(by.repeater("org in orgs")).filter(function (elm) {
return elm.evaluate("org.type").then(function (orgType) {
if (orgType === "ORG_TYPE_FOO") {
return orgType;
}
});
}).then(function (elms) {
//...
});


Attempt 1

I've tried this but evaluate() doesn't seem to like two parameters. orgName always returns
undefined


element.all(by.repeater("org in orgs")).filter(function (elm) {
return elm.evaluate("org.type", "org.name").then(function (orgType, orgName) {
if (orgType === "ORG_TYPE_FOO" && orgName === "NAME1") {
console.log(orgType + " ------ " + orgName);
return orgType;
}
});
}).then(function (elms) {
//...
});


Attempt 2

I was thinking that I could filter the elms again after the first filter+evaluate but I cant seem to get that to work since elms is an
ElementArrayFinder


element.all(by.repeater("org in orgs")).filter(function (elm) {
return elm.evaluate("org.type").then(function (orgType) {

if (orgType === "ORG_TYPE_FOO") {
return orgType;
}
});
}).then(function (elms) {

elms.filter(function (elm) {
return elm.evaluate("org.name").then(function (orgName) {

if (orgName === "NAME1") {
return orgName;
}
});
}).then(function (elms) {
//...

});

});

Answer

You can use protractor.promise.all() method to resolve multiple promises at once.parameter for the above method is to pass array of promises and it will give the output as array.In your case you can use something like below,

element.all(by.repeater("org in orgs")).filter(function (elem) {
  return protractor.promise.all([elem.evaluate("org.type"),elem.evaluate("org.name")]).then(function (resultArray) {
        return (resultArray[0] === "ORG_TYPE_FOO" && resultArray[1] === "NAME1") 
    }
  });
}).then(function (elms) {
//...
});