7

How can I get the ng-model in ng-repeat with protractor ?

<div ng-repeat="field in master.linker | orderBy:'country.name'">
    <div>
        <p> {{ field.country_name }} </p>
        <input ng-model="field.text">
    </div>
</div>

I use this, but without success :

var result = element.all(by.repeater('field in master.linker').column('field.text'));

result.forEach(function(entry) {
    console.log(entry);
});

I would like to compare :

result.forEach(function(entry) {
    if (entry.country_name === 'en') {       
        expect(entry.text (from ng-repeat)).to.eventually.equal(value)
    }
});
Jérémie Chazelle
  • 1,721
  • 4
  • 32
  • 70

1 Answers1

5

The .column() would only work for bindings, not the models.

In your case, use the by.model() locator:

var result = element.all(by.repeater('field in master.linker'));

result.each(function(entry) {
    var input = entry.element(by.model("field.text"));

    // do smth with the input
});

If you want to get the input values, use map():

var inputValues = result.map(function(entry) {
    return entry.element(by.model("field.text")).getAttribute("value");
});

// printing out input values
inputValues.then(function (values) {
    console.log(values);
});

Answering additional question from a comment:

I have an array, without other fields from my ng-repeat, how can I compare "if (field.country_name === ""en") { expect(field.text).to.eventually.equal(value)}" ?

Use filter():

var fields = element.all(by.repeater('field in master.linker'));
fields.filter(function (field) {
    return field.element(by.binding("field.country_name")).getText().then(function (country) {
        return country === "en";
    });
}).then(function (filteredFields) {
     var input = filteredFields[0].element(by.model("field.text"));
     expect(input.getAttribute("value")).to.eventually.equal(value);
});;
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I have an object ptor : { ptor_: { controlFlow: [Function], – Jérémie Chazelle Dec 16 '15 at 18:06
  • @JérémieChazelle if you are trying to print out `input`, sure - you'll get that since this is an `ElementFinder` instance. What is your desired output? The values of the inputs? Thanks. – alecxe Dec 16 '15 at 18:07
  • I have an array, without other fields from my ng-repeat, how can I compare "if (field.country_name === ""en") { expect(field.text).to.eventually.equal(value)}" ? – Jérémie Chazelle Dec 17 '15 at 09:05
  • @JérémieChazelle okay, updated the answer, hope that helps. – alecxe Dec 17 '15 at 15:06