使用Protractor获取ng-repeat ng-repeat

时间:2015-12-16 17:46:02

标签: javascript angularjs protractor bdd end-to-end

如何使用量角器获得ng-repeat的ng-model?

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

我用这个,但没有成功:

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

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

我想比较一下:

result.forEach(function(entry) {
    if (entry.country_name === 'en') {       
        expect(entry.text (from ng-repeat)).to.eventually.equal(value)
    }
});

1 个答案:

答案 0 :(得分:5)

.column()仅适用于binding s,而不适用于model

在您的情况下,请使用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
});

如果您想获取输入值,请使用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);
});

回答评论中的其他问题:

  

我有一个数组,没有来自ng-repeat的其他字段,如何比较“if(field.country_name ===”“en”){expect(field.text).to.eventually.equal(value) )}“?

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