2

I'm in the need to assert the number of the children .row of a series of divs .container is greater than 0.

this.Then(/^I should see at least one element in each container$/, function (done) {  
    page.element.all(by.css('.container'))                                                    
        .each(function (element, index) {                                     
            element.all(by.css('.row'))                               
                .then(function(elements) {                                        
                    expect(elements.length).to.be.greaterThan(0);                   
                    done();                                                       
                });                                                               
    });
});                                                                   

The markup looks like:

<div class="container">
    <div class="row"></div>
    <div class="row"></div>
    <div class="row"></div>
</div>
<div class="container">
    <div class="row"></div>
</div>

and there must be at least one .row in each container.

The problem is that it always passes, as soon as the first checks true.

I've tried different solutions using protractor.promise object, also trying to save the promise within the each iteration and then trying to do the expectation at the end but nothing really worked.

I've started thinking that I should take this approach from a completely different perspective.

Any takers?

Mr Peach
  • 1,364
  • 1
  • 12
  • 20

1 Answers1

0

You should be getting the row elements under each div and then assert if there are more than zero rows in each div. Try this -

this.Then(/^I should see at least one element in each container$/, function () {  
    page.element.all(by.css('.container'))                                                    
        .each(function (ele, index) {                                     
            ele.$$('.row')     // get all the row elements in each div                           
                .then(function(elements) {                                        
                    expect(elements.length).to.be.greaterThan(0);                                                                         
                });                                                               
    });
});  

Hope this helps

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • I thought that `ele.$$('.row')` is effectively the same as `ele.all(by.css('.row'))` :-/ unfortunately it still exits as long as the first `expect` returns true. – Mr Peach Sep 18 '15 at 16:11
  • OK, seen your last edit which removed the `done()` call. I now get `AssertionError: expected 1 to be above 1` – Mr Peach Sep 18 '15 at 16:45
  • I guess that's because you have only 1 row in the second div. Can you tell me where is this error coming up from? I mean is it for the first div with 3 rows or second div with 1 row. And your expect error looks like you are expecting something to be greaterThan(1). – giri-sh Sep 19 '15 at 10:51
  • I'm testing this with 11 and 12 rows in each container. That's why this to me doesn't make sense at all. – Mr Peach Sep 19 '15 at 16:49
  • OUCH, found out the error I was looking at was coming from a typo. Now, without the `done()` I get: `BUG: launcher exited with 1 tasks remaining` – Mr Peach Sep 21 '15 at 10:29
  • And what happens if you put in `done()` call at the end? I guess you should put done() when all your expectations are complete ouside `element.all()` not sure if it helps. – giri-sh Sep 21 '15 at 10:55
  • so I do see the tests passing, but I don't know whether it's just passing because it goes straight to `done()` or what. If I `console.log(elements.length)` I do get both printed, so it might actually be it... I don't know much of what's behind it. – Mr Peach Sep 21 '15 at 12:45