Arrays in UI vision Selenium IDE++

hi! can i use arrays in UI vision?

1 Like

Yes! See here: Nested Variables or Variables Inside Variables Requested

Plus, the DemoExecuteScript macro that ships with UI Vision has a demo of using an array.

Hi. Thank you for replay. Is it possible to convert array to csp string or to json? also convert string or json ton array?

For CSV (string) to array see here: How to read a CSV file with a changing number of columns

Array to string: What would be the purpose of this? It can be done, but the method depends on what you want to achieve.

V5.5.6 adds Selenium IDE style array support e. g. ${a[0][5]} and a new way to import/export arrays to/from CSV files.

To create a Javascript array, use executeScript_Sandbox executeScript command:

executeScript | 
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 | myArray

To loop over an array you can use forEach.

The example below creates a 5x3 array, stores the values in the myArray array variable and then saves the array to a CSV file.

Fully demo macro:

Update for V7: The overall script remains the same but we replaced all executeScript_Sandbox with executeScript because the new _sandbox version can not return arrays. We use this feature in the macro e. g return arr or return newArr.

{
  "Name": "DemoArray",
  "CreationDate": "2022-4-19",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "comment",
      "Target": "Create an array and save the content to a CSV file",
      "Value": ""
    },
    {
      "Command": "executeScript",
      "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": "comment",
      "Target": "Manually set two array values ",
      "Value": ""
    },
    {
      "Command": "executeScript",
      "Target": "var newArr = ${array1}; newArr[0][2] = 'Hello World'; return newArr",
      "Value": "array1"
    },
    {
      "Command": "executeScript",
      "Target": "var newArr = ${array1}; newArr[2][1] = 'This is how you set an array value'; return newArr",
      "Value": "array1"
    },
    {
      "Command": "csvSaveArray",
      "Target": "array1",
      "Value": "data_from_array.csv"
    }
  ]
}

Important reminder: For working with arrays you must use executeScript, not executeScript_Sandbox.

Currently executeScript_Sandbox can not return arrays.