0

I want to get all the values from a registration page and store all values in an array. How can I do that in protractor?

var arr = new Array();  //declare array 
InputName.getAttribute("value")
         .then(function(value){
            arr[0]=value;  // want to store promise value in an array
         });
console.log(arr[0]);
barbsan
  • 3,418
  • 11
  • 21
  • 28
hello test
  • 11
  • 1
  • 1
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – barbsan May 21 '19 at 08:03

1 Answers1

0

If you run your code, it will first log arr[0] and then resolve Promise. Therefore, you may access that array's values in the next Promise. Something like this

var arr = new Array();  // <- this is by the way bad practice, use 'let arr = [];'
InputName.getAttribute("value")
         .then(function(value) {
            arr[0]=value;  // I would use arr.push(value)
         });
anotherInput.getAttribute("value")
         .then(function(value) {
            console.log(arr[0]); // your value should be accessible here
            arr.push(value) // push another value
         });

But, honestly, I've been working with Protractor fo a while now and I still have difficulties understanding promises... This why I'm using async/await in my tests so if I were to implement something like that I would end up having the following

let arr = [];
let value1 = await InputName.getAttribute("value");
arr.push(value1);
console.log(arr[0]);

Clear, neat code with no hustle. Plus protractor team is actually removing promise_manager, so one day when you update it your code will not work anymore. Then why not switch earlier

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40