2

Following this example: Protractor get element by model in repeater array, I am running into a head scratcher. I want to send input value to an element inside of a repeater. Hopefully it is a simple solution!

My HTML looks similar to this. The main difference being "posts" vs "user.posts".

<tr ng-repeat="post in user.posts">
    <td ng-click="activePost(post)" class="title">{{post.title}}</td>
    <td><button class="btn btn-danger" ng-click="delete(post)">Delete</button></td>
  <td><input type="text" ng-model="post.subtitle" 
        id="{{post.id}}" /></td>
 </tr>

Then following the example, I wrote something like this:

element.all(by.repeater('post in user.posts')).then(function(posts) {
   var activePost = posts[0].element(by.model('post.subtitle'));
   activePost.sendKeys('My post');
});

The protractor test fails at "activePost.sendKeys('My post')" with the following error:

NoSuchElementError: No element found using locator:  
by.model("post.subtitle"). 
Stacktrace:
         NoSuchElementError: No element found using locator: by.model("post.subtitle")
        at Array.forEach (native) Error
        at /Users/.../e2e/spec.js:51:17
        at Array.forEach (native) From: Task: Asynchronous test function: it() Error
        at [object Object].<anonymous> (/Users/.../e2e/spec.js:32:3)
        at Object.<anonymous> (/Users/.../e2e/spec.js:2:1)

Would anyone have any suggestions for what I could try or a different way to think about it? Thanks!

ANSWER: The error Protractor was throwing was completely unrelated to the repeater but instead to a missing ending div tag in the view. Isn't it occasionally something odd like that? Go figure.

Community
  • 1
  • 1
user3495469
  • 139
  • 2
  • 11

1 Answers1

0

There is no need to resolve promises here, just chain element and element.all():

var elm = element.all(by.repeater('post in user.posts')).first().element(by.model('post.subtitle'));
elm.sendKeys('My post');

Note that I'm using first() here.


Note that a simple by.tagName() locator would also work here:

var elm = element.all(by.repeater('post in user.posts')).first().element(by.tagName('input'));
elm.sendKeys('My post');
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I tried what you suggested but I still get the same error. Do you have any other thoughts? – user3495469 May 11 '15 at 19:50
  • @user3495469 do you have other `post in user.posts` repeaters on the page? Do you see any iframes on the page? Thanks. – alecxe May 11 '15 at 19:51
  • good call! i do have another repeater on the page, however it is 'entry in user.posts' vs 'post in user.posts'. Would that make a difference? There is no iframe on the page. – user3495469 May 11 '15 at 19:58
  • still the same error, could there be an issue if the ng-model="post.subtitle" is referenced in more than one place? I am testing that theory out. – user3495469 May 11 '15 at 20:15
  • @user3495469 yeah, but the other approach I've provided (using tag name) also doesn't work for you, does it? – alecxe May 11 '15 at 20:22
  • i have more than one input element on my page so i wouldn't be able to use the second option. I went back to my html view and found some unrelated error where a closing div tag went missing. and i ran protractor again and I have no idea why but it now works! thanks for your help! – user3495469 May 11 '15 at 20:29
  • @user3495469 wow, didn't see that coming :) See if it makes sense to accept the answer or not. Thanks. – alecxe May 11 '15 at 20:31