Using script variables inside Macro variable, csvReadArray

Hi all,
I’m working with a csv and filling some fields in a browser.
I have a script like this
the ToDo is the result of a csvReadArray.

var i = 1; return ${ToDo[1][3]};

Which works just fine, but I’d like to use the var i inside the ${ToDo[ i ][3]}
And I can’t find the way for this to work.
PD. This is a simplified code, that i is actually inside a while and more other stuff is happening. But I encountered that this simple script won’t run and says: " [error]
Error in executeScript code: Unexpected token ‘{’ "

var i = 1; return ${ToDo[i][3]};

Is this inside executeScript? Then we live in the Javascript world and do not need {}. This works ok:

var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; var ii =2; return arr[ii][2]

Inside Selenium IDE and UIVision, this works:

echo | ${todo[${i}][2]}

Macro:

{
  "Name": "Array-1",
  "CreationDate": "2020-6-10",
  "Commands": [
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; return arr",
      "Value": "array1"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; var ii =2; return arr[ii][2]",
      "Value": "value"
    },
    {
      "Command": "echo",
      "Target": "value=${value}",
      "Value": "green"
    },
    {
      "Command": "times",
      "Target": "5",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "Substract 1 from !times, as the array index starts with 0",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${!times} - 1;",
      "Value": "i"
    },
    {
      "Command": "echo",
      "Target": "Row ${i}, 3rd Element => ${array1[${i}][2]}",
      "Value": "blue"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

YES, it is insnide executeScript, but ToDo array is a Macro variable that I get from a CsvReadArray
What I want to do is to acces to that index of the array from a JS variable, and as I just said earlier the i vaiable is inside a loop, is not as simple.

@trexem

Why do not use a better solution to fill field from a csv ?

It’s a simple work copy from csv and fill fields do not need complex macro code.

I suggest always simple macro and few code lines this give speed and few chances of errors.

because i need to verify data in other fields, if it’s a match then I fill it. To do this I’m using loops, I can do it with kantu while, but it’s too slow, that’s why I wanted to do the loop part in javascript.

I tried again with outside variables and it works, too. See here:

If it does not work in your case, can you post a test macro?

  {
  "Name": "a2-1",
  "CreationDate": "2020-6-10",
  "Commands": [
    {
      "Command": "store",
      "Target": "2",
      "Value": "u"
    },
    {
      "Command": "comment",
      "Target": "Test1: Array from within JS",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; var ii =2; return arr[${u}][2]",
      "Value": "value1"
    },
    {
      "Command": "echo",
      "Target": "value1=${value1}",
      "Value": "green"
    },
    {
      "Command": "comment",
      "Target": "Test1: Array from outside",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; return arr",
      "Value": "array1"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${array1[${u}][2]}",
      "Value": "value2"
    },
    {
      "Command": "echo",
      "Target": "value2=${value2}",
      "Value": "pink"
    }
  ]
}

not a JS array with macro variable, the other way, Macro array with JS variable

{
  "Name": "test",
  "CreationDate": "2020-6-10",
  "Commands": [
    {
      "Command": "store",
      "Target": "2",
      "Value": "n"
    },
    {
      "Command": "csvReadArray",
      "Target": "C:\\Kantu\\csv\\FoliosToDo.csv",
      "Value": "ToDo"
    },
    {
      "Command": "executeScript",
      "Target": "var i = 1; return ${ToDo[i][3]};",
      "Value": "n"
    }
  ]
}

If you explain better what you need to do you can make a better macro than the one you have made.

Explain what you need to do and indicate the urls of the pages so that you can understand and recommend a better code for your macro. I fill out so many forms without any problem so it’s really weird that you have all these problems for such a simple thing. Your macro needs to be optimized.

I confirmed that there is a problem using “outside” (Selenium IDE defined) arrays inside execute_script with a JS variable. But I found a solution/workaround: Copy (clone) the “outside array” (here: array1) to a JS array (here: brr):

brr= Array.from(${array1});i=2;return brr[i][2]

Macro:

{
  "Name": "arraycopy",
  "CreationDate": "2020-6-10",
  "Commands": [
    {
      "Command": "store",
      "Target": "2",
      "Value": "u"
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "!errorignore"
    },
    {
      "Command": "comment",
      "Target": "Test1: Array from outside, variable from JS => error ",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; for(var x = 0; x < 5; x++){arr[x] = []; for(var y = 0; y < 3; y++){arr[x][y] = (x+1)*(y+1);}}; return arr",
      "Value": "array1"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "ii=2;return ${array1[ii][2]}",
      "Value": "value2"
    },
    {
      "Command": "echo",
      "Target": "value2=${value2}",
      "Value": "blue"
    },
    {
      "Command": "comment",
      "Target": "Solution: Copy outside array to JS array => works ",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "brr= Array.from(${array1});ii=2;return brr[ii][2]",
      "Value": "value2"
    },
    {
      "Command": "echo",
      "Target": "value2=${value2}",
      "Value": "pink"
    }
  ]
}
1 Like

Thank you, I was trying to do something like this as workaround but got distracted with other work. I’ll try it, thank you so much.

I apologize for my intervention but I did not understand the problem of the user and therefore I asked for more details to find a solution to its automation.