RunScript returning multiple values not always work

Hi,

I want to return multiple values from JS in runScript. It’s impossible to return many values at once so i use a little trick and try something like this:

runScript | var i = 1; var j = 2; var k = 3; |
runScript | return i; | i
runScript | return j; | j
runScript | return k; | k

In first run, command with return “i” throw an error that “i” is not declared. So i change first runScript to addScript and then it’s works fine. Furthermore when i change it back to runScript it work fine from now on. It’s just happening from time to time. It’s not a big deal because after change command ot works fine but what if it will happen in real live situation not just checking how to return many values? It could be frustrating then.

I found a way around this problem i think. I found this topic: Array in Katalon Recorder and i modify it a little bit to work in actual version of Katalon. And then i modifed it even more so i have two way to workaround this problem.

1 version:

runScript | var i = 1; var j = 2; var k = 3; |
storeEval | 0 | loop
while | ${loop}<=2 |
storeEval | array[${loop}] | element${loop}
storeEval | ${loop} + 1 | loop
endWhile

This version is good if you have a lot of values to store in Katalon from just one JS code.

2 version:

runScript | var i = 1; var j = 2; var k = 3; |
storeEval | array[0] | element0
storeEval | array[1] | el1
storeEval | array[2] | something

This is bettter if you want to give your vars diffrent names like shown in the example. It 's also easier to understand to people how are new in Katalon.

When working with JavaScript variables and runScript, you need to use the window prefix

Here’s my solution

open | http://forum.katalon.com/t/runscript-returning-multiple-values-not-always-work/17589 | |
runScript | window.i = 1; window.j = 2; window.k = 3; | |
runScript | return window.i | i |
runScript | return window.j | j |
runScript | return window.k | k |
echo | i = ${i} j = ${j} k = ${k} | |

Running this produces the Log

[info] Executing: | open | http://forum.katalon.com/t/runscript-returning-multiple-values-not-always-work/17589 | |
[info] Executing: | runScript | window.i = 1; window.j = 2; window.k = 3; | |
[info] Executing: | runScript | return window.i | i |
[info] Store '1' into 'i'
[info] Executing: | runScript | return window.j | j |
[info] Store '2' into 'j'
[info] Executing: | runScript | return window.k | k |
[info] Store '3' into 'k'
[info] Executing: | echo | i = ${i} j = ${j} k = ${k} | |
[info] Expand variable 'i = ${i} j = ${j} k = ${k}' into 'i = 1 j = 2 k = 3'
[info] echo: i = 1 j = 2 k = 3
2 Likes