How to access input at item level in protractor by

2019-04-16 20:39发布

问题:

Not Able to access angular input by protractor by model

Have been through post

<div ng-repeat="todo in todos">
  <!--i can access span-->
  <span>{{todo.text}}</span>
  <span>{{todo.address}}</span>
  <span>{{todo.done}}</span>

  <!--i CAN'T access input -->
  <input type="text" ng-model="todo.text"/>
  <input type="text" ng-model="todo.address"/>
  <input type="text" ng-model="todo.done"/>
</div>

Protractor Javasript I Can access Span At Item Level But Cannot access Input

var result= 
browser.findElements(protractor.By.repeater('todo in todos').column('todo.text'));

/*For Span at item level It works*/
/*
result.then(function(arr) {
   arr[0].getText().then(function(text) {
    console.log(": "+ text);
  });
});*/

For Input at item level It dosen't works

Try 1 : typeError: Cannot call method 'getText' of undefined

/*
result.then(function(arr) {
   arr[0].getText().then(function(text) {
    console.log(": "+ text);  //TypeError: Cannot call method 'getText' of undefined
  });
});*/

Try 2 : typeError : Cannot call method then of undefined

How to access model according to

How protractor access to model

result.then(function(arr) {
  arr[0].then(function(text) {
    console.log(": "+ text.getAttribute('value')); 

  });
});

Not Able to find What Wrong I Am doing ..Is There Any other way to access Angular input.

回答1:

After trying for a long time I was able to get this working:

element(by.repeater('todos')).evaluate('todos.length').then(function(itemLength)
{
    for(var i = 0 ; i < itemLength ; i++)
    {
        var result= element(by.repeater('todo in todos').row(i));

        result.then(function(arr)
        {                     
            /*FETCH TEXT*/
            var m = arr.all(by.model("todo.text")).getAttribute('value');
            m.then(function(l){
                console.log(l);
            });

            /*CLEAR TEXT*/
            arr.all(by.model("todo.text")).clear();

            /*SET TEXT*/
            arr.all(by.model("todo.text")).sendKeys('Hello');
        });
    };
});

Doubts :

  1. I know about Angular promise..then .i have nested three Angular promise..then one inside another but don't know why use then in protractor.
  2. Below "evaluate" just returns itemcount.


标签: protractor