Search code examples
javascriptangularjsprotractorbddend-to-end

Get ng-model in ng-repeat with Protractor


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)
    }
});

Solution

  • 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);
    });;