-2

simple JavaScript counter is not working.

I am using JavaScript within protractor (angular testing framework) but I don't think it's the protractor issues but JavaScript issues.

I thought that I can do this: (pseudo code actual code is the one below)

var counter = 0

foreach (smallitems in bigitems){
    function(smallitems){foreach (item in smaillitems){
        if(item == "somevalue") counter++ }
    }
}

but counter never grows evenwhen there's matching values.

Actual codes are:

'use strict';

describe('list page ', function () {

    it('should list page', function() {

        var counter = 0;

        element.all(by.repeater('page in pages')).each(function (page) {
            element.all(by.repeater('item in items')).each(function (item) {
                if (item.getText() == ("A+")) {
                    counter++;
                }
            })
        })
    })
})

counter always 0. Do you see the problem here?

thank you!

CodeSchool JJ
  • 585
  • 5
  • 16

2 Answers2

3

item.getText() is a webdriver Promise [Object object] so it will never == "A+"

You need to follow promises when not using expect. The reason why expect(item.getText()).toEqual("A+") would work is because expect resolves promises for you and performs the string comparission with "A+" in this case.

Moving back to the issue at hand, it looks like you're trying to count things to perform an expectation on the total count:

describe('count on filtered elements', function() {
    // page objects
    var pagesRepElms = element.all(by.repeater('page in pages'));
    var itemsLocator = by.repeater('item in items');

    // since Protractor 1.3 more interesting page objects can be stored
    var aPlusElms = pagesRepElms.all(itemsLocator).filter(function(item) {
        return item.getText().then(function(text) {
            return text === 'A+';
        });
    });

    it('gathers and performs the count expectation in 1 line', function() {
      // modify 5 with your expected count
      expect(aPlusElms.count()).toEqual(5);
    });
});
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
  • I don't have time to try this out at the moment. but getting strong feeling this is IT!. may I ask you where did you spot "// this need to be here if you want to preserve state between it() specs "? Is there docs I am missing :) Big thank you. – CodeSchool JJ Oct 08 '14 at 04:42
  • That's not in the docs but is how Jasmine works regarding scope and expectations on top of promises. I've simplified the answer to the best solution, good luck ;) – Leo Gallucci Oct 08 '14 at 04:48
1

First you need to fix your missing closing brackets. foreach should just be for. And inside our for loop we say: smaillitems[item] and not just item. Same for the outer loop. Finnaly we don't want to use function, that's defines a new function, it's not called, so we don't need it:

for (smallitems in bigitems) {
    for (item in bigitems[smallitems]) {
        if(bigitems[smallitems][item] == "somevalue"){ counter++; }
    }
}  
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • whoops sorry. I was pseudo coding. (at least for the first one) which I believe contains the same logic as one below. strangely though, the one at the bottom never increments the counter. – CodeSchool JJ Oct 08 '14 at 03:59
  • Just fixed, I should of said `bigitems[smallitems][item]` when I said `smallitems[item]`. – Spencer Wieczorek Oct 08 '14 at 04:00
  • Thanks for showing me the correct way. and yet as for the protractor one, it doesn't accept the logic you game me. I was searching for the logic behind and wasn't successful. – CodeSchool JJ Oct 08 '14 at 04:03
  • It seems to be working in this fiddle:http://jsfiddle.net/ef9aaaf2/1/. Note I didn't say `var counter = 0;` above. – Spencer Wieczorek Oct 08 '14 at 04:05
  • I checked to the point that there are actually matching elements (that item.getText() == ("A+") becomes true) among items. – CodeSchool JJ Oct 08 '14 at 04:05
  • I tried out working sample within my protractor setting. it throws errors saying bigitems is not defined. One thing more, I am user 'use strict'; mode – CodeSchool JJ Oct 08 '14 at 04:10
  • @CodeSchoolJJ Where/how did you define `bigitems`? – Spencer Wieczorek Oct 08 '14 at 04:11
  • bigitems is within "describe", "it" function blocks (jasmine framework requires these blocks and the same for the protractor). So my javascript is within the function block and this make things bit more complicated. – CodeSchool JJ Oct 08 '14 at 04:14
  • Can you explicitly define a `bigitems = [...]` for testing purposes and see if that works? – Spencer Wieczorek Oct 08 '14 at 04:18
  • yes I checked the array and that it's matching becomes true. ) ( this part: item.getText() == ("A+")) for the bigitems example, I cannot use without var bigitems. thank you again. might need to leave it for now :) * I edited original code bit more – CodeSchool JJ Oct 08 '14 at 04:23