[Solved] [Issue #408] How do I change a value in an array?

lets say I have this:
storeEval | new Array (“one”,“two”,“three”) | array

How do I change the value on the second item?

storeEval | storedVars[‘array’][2]=“five” |

doesn’t work

Anyone can give some input, please?

LoL.Have you tried it before posting? :grin:

Here is a code snippet that first fills an array, and then changes a value with
storeEval | storedVars['names'][${num}] = "Hello World!"

{
  "CreationDate": "2018-10-30",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'].length",
      "Value": "length"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}]",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is ${myrandomname}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] = \"Hello World!\"",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is now ${myrandomname}",
      "Value": "green"
    }
  ]
}

This doesn’t change anything. This is the problem. You just assign $myrandomname a value FROM the array and call it for echo. JavaScript code doesn’t actually work in StoreEval just gets the void.
Check for yourself. Just call it once again. It is fish and always was. Sic!
{
“CreationDate”: “2018-10-31”,
“Commands”: [
{
“Command”: “store”,
“Target”: “fast”,
“Value”: “!replayspeed”
},
{
“Command”: “open”,
“Target”: “Page Not Found”,
“Value”: “”
},
{
“Command”: “storeEval”,
“Target”: “new Array (‘cat’,‘dog’,‘fish’,‘dog’,‘??’,‘frog’,‘?’,‘dog’,‘??’,‘horse’,‘??elephant’)”,
“Value”: “names”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’].length”,
“Value”: “length”
},
{
“Command”: “store”,
“Target”: “2”,
“Value”: “num”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}]”,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is ${myrandomname}”,
“Value”: “blue”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}] = "Hello World!"”,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is now ${myrandomname}”,
“Value”: “green”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}]”,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is now ${myrandomname}”,
“Value”: “red”
}
]
}

You are right - I see the issue now. To me, that looks like bug, and I made a ticket for it.

{
  "CreationDate": "2018-10-31",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}]",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is ${myrandomname}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] = \"Hello World!\"",
      "Value": "notused"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] ",
      "Value": "newname"
    },
    {
      "Command": "echo",
      "Target": "in array is now ${newname}",
      "Value": "green"
    }
  ]
}
1 Like

We investigated the problem further.

By design, it is not possible in Kantu to update one element in an array in the way you and I tried in the test macro above.

The reason is that a statement like "storedVars['names'][${num}] = \"Hello World!\"" will be expanded to ["cat","dog","fish","dog","??","frog","?","dog","??","horse","??elephant"][2] = "Hello World!" during runtime.

So basically, all things in target of storeEval all happen in guest page context, variables in Kantu won’t be affected. And I think we should stick to this rule.

But we found a good solution (see next post).

Solution:

You can achieve the goal of setting one element in an array by using the javascript array api function splice, and replace the whole array in kantu. And then assign it to the array again:

arr = storedVars['names'], arr.splice(${num}, 1, "Pink Elephant"), arr

If num = 2, this changes the 2nd element of the array from “dog” to “pink elephant”.

array1

Complete test macro:

{
  "Name": "replace element in how array",
  "CreationDate": "2018-12-9",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "comment",
      "Target": "change value 2 of the array from \"dog\" to \"pink elephant\"",
      "Value": "green"
    },
    {
      "Command": "storeEval",
      "Target": "arr = storedVars['names'],  arr.splice(${num}, 1, \"Pink Elephant\"), arr",
      "Value": "names"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] ",
      "Value": "newname"
    },
    {
      "Command": "echo",
      "Target": "The ${num}nd element of the array is now ${newname}",
      "Value": "pink"
    }
  ]
}
1 Like

storeEval is deprecated. Use “ExecuteScript” instead.

Full array support arrived with V5.5.6, see Arrays in UI vision Selenium IDE++ - #5 by admin

To change a value in an array, you need to use ExecuteScript + Javascript. This line does it:

executeScript | var b = ${arr}; b[${i}][2] = "done"; return b; | arr

Explanation:

  • Move the array from the Selenium IDE array variable (here called “arr”) into a Javascript array (here called “b”, code: var b = ${arr}; )

  • change the cells that you want to change (here b[${i}][2] = "done"; )

  • and then return the modified array back into the Selenium IDE variable (here return b; )

For a complete test macro see How to mark a row in a CSV line as processed?

1 Like

I’VE BEEN LOOKING FOR THIS FOR WEEKS!
Thank you