0

I'm new to node.js and protractor end-to-end testing and might be I don't know something really simple, but I just couldn't find an answer.

I have a for loop in which I want to get data from a function which returns a promise. Code chunk looks like this:

for (var i=2; i<Math.ceil(searchTotal/pageSize)+1; i++) {
    checkNext(i, pageSize, searchTotal).then( function(pageData){
        console.log(pageData + ' PENDING ');
        if (!pageData.isCorrect) {
            console.log(pageData + ' FAILED');
            expect(pageData.searchText).toEqual(pageData.formedText);
        }
        else if (i == Math.ceil(searchTotal/pageSize)) {
            console.log(pageData + ' SUCCEEDED');
            expect(pageData.searchText).toEqual(pageData.oldText);
        }
    })
}

Thing is, pageData is undefined all the time.

Would be glad if somebody could point out my mistake.

gosferano
  • 25
  • 4
  • 1
    `checkNext` is probably returning a promise with an undefined value. –  Feb 03 '16 at 10:39
  • please provide us with the checkNext function – Carsten Feb 03 '16 at 10:44
  • Downvoter: You're free to downvote, although the suggested reasons are "not useful" and "shows no research effort" which may or may not apply. The fact that not enough information is provided is less a reason for downvoting than a reason for closevoting (which perhaps you already did). I'm just saying, let's be careful not to confuse downvotes with closevotes, and not to abuse the former. –  Feb 03 '16 at 12:08

2 Answers2

1

Make sure you have a return in checkNext. If you don't explicitly return something, a promise will return undefined.

martin770
  • 1,271
  • 11
  • 15
  • The lack of a return in `checkNext` would cause it to return `undefined`, and then it would complain about trying to call `then` on undefined. The problem is more likely that `checkNext` **is** returning a promise, which is resolved, but with an undefined value. –  Feb 03 '16 at 13:52
  • It would complain about trying to call `then` on undefined if `checkNext` is not a promise. It would complain about the promise resolving to `undefined` if the promise did not resolve with a value (what I meant by `return`). – martin770 Feb 03 '16 at 19:45
  • Assuming `checkNext` starts off with something like `return new Promise(...`, then the value of that promise would not be set by a `return` statement anywhere, but rather by the value passed to a call to `resolve()`. –  Feb 03 '16 at 19:51
0

One of the issues you may be facing is async looping. Async looping is hard. Luckily, there are many, many answers here on Stack. Because it's async, node will burn through your loop and then pass only the last value.

With the code provided, I can't offer a specific example, but one general example (see other answers for more ways) is to use a function and pass in your index. This can even be an anonymous function that immediately fires. Eg:

for(var i = 0; i < somethings.length; i++) {
    (function(i) {
        expect(someOtherThing.get(i).isDisplayed()).toBe(true);
    })(i);
}

Hope that helps

Community
  • 1
  • 1
Brine
  • 3,733
  • 1
  • 21
  • 38