-1

I'm having issues with variable scope. The code below is in Protractor which is a mix of Jasmine and JavaScript. The issue that I'm having is that I'm trying to use string value from householdLabelText and compare it to the value of householdArray[i].

However, whenever I do this, householdArray[i] comes back as undefined. I'm guessing this is because householdArray goes out of scope or something. I'm not exactly an expert how the Promise/callback thing works, so I may be wrong. If anyone could give me some tips on how to keep householdArray in scope, I would appreciate it.

it((testNumber += 1) + '---' + suiteName + '---' +
'It should dispaly the appropraite text for the rating factors.',
    function() {

        var householdArray = ['ACCIDENTS', 'VIOLATIONS', 'DRIVERS', 'VEHICLES', 'TENURE'];

        element.all(by.repeater('item in vm.policyDetails.householdDetails')).then(function(householdDetails) {

            for(var i = 0; i < householdDetails.length; i++)
            {
                householdDetails[i].element(by.binding('item.label')).getText().then(function(householdLabelText) {
                console.log(householdArray[i]);
                expect(householdLabelText).toEqual(householdArray[i]); // householdArray[i] is undefined
                });
            }
        });
    });
DrZoo
  • 1,574
  • 1
  • 20
  • 31
  • I'm voting to close this question as off-topic because you need to learn how to use the step debugger, the step debugger is your friend! –  Jul 19 '16 at 23:15
  • Can you post an example ([codepen](codepen.io)) of your code that produces the problem? – Trevor Jul 19 '16 at 23:21
  • @JarrodRoberson I don't have an IDE or a way to use a step debugger. If you can recommend me a step debugger for Protractor, then I'll use it. – DrZoo Jul 19 '16 at 23:28
  • @threed I added a console.log() where it would print undefined. – DrZoo Jul 19 '16 at 23:31
  • @JarrodRoberson I'm guessing he's not very familiar with step debuggers, since he didn't realize they were baked into browsers, which I think indicates that he's just uncertain, not lazy. – Trevor Jul 19 '16 at 23:36
  • *[codepen](http://codepen.io/) – Trevor Jul 19 '16 at 23:41
  • @JarrodRoberson Protractor is not a like a typical language. I don't know of any IDE's to use with it. And a step debugger in the browser doesn't work. When running a test, if you open the debugger it closes the test immediately. – DrZoo Jul 19 '16 at 23:49
  • @JarrodRoberson would you also like to link me to the duplicate? – DrZoo Jul 19 '16 at 23:52
  • @DrZoo The duplicate is now showing up at the top of your question. – Trevor Jul 19 '16 at 23:55
  • @threed I disagree at the duplicate, but whatever. – DrZoo Jul 20 '16 at 02:39
  • I didn't choose the duplicate, but it does seem possible that it's closely related. Without a running example of your code, it's difficult to help much further. Please consider creating a CodePen that produces the same issue. – Trevor Jul 20 '16 at 02:44
  • Use array.forEach(function(Value,index,arr){}) method instead of for loop in your code. It will perfectly work – Optimworks Jul 20 '16 at 03:16
  • @SureshSalloju I'll give it a shot when I get back to work tomorrow. – DrZoo Jul 20 '16 at 03:30
  • @SureshSalloju that worked. Thanks! You can move it to an answer and I'll mark it. – DrZoo Jul 20 '16 at 15:21
  • @DrZoo cool.Nope yaar – Optimworks Jul 20 '16 at 15:29

1 Answers1

0

The scope of householdArray is fine since it has been declared in the test function block and you are using it in a nested function so it should absolutely be in scope.

What is a more likely cause is that the value of i causes it to read beyond the array bounds because householdDetails.length might be different than householdArray.length. That's the first thing I would check in this instance.

JVDL
  • 1,157
  • 9
  • 16