Hi all,
I created a script to construct a new variable (text) thanks to multiple extracted variables (ID, NUM, DATE).
I am able to see the new variable on debug log.
But the value return by the function is null.
Please help.
*** SCRIPT START ***
function evaluate () {
var i = 1;
var text = "";
var max = context.variableManager.getValue("ID_matchNr");
while (i <= max) {
var value1 = "ID_"+i; //Extracted variable ID
var value2 = "NUM_"+i; //Extracted variable NUM
var value3 = "DATE_"+i; //Extracted variable DATE
var value_id = context.variableManager.getValue(value1);
var value_num = context.variableManager.getValue(value2);
var value_date = context.variableManager.getValue(value3);
text += "{\"id\":"+value_id+",\"num\":"+value_num+",\"date\":"+value_date+"}";
if (i<myvalue) {
text += ",";
}
i++;
}
logger.debug(text);
return text;
}
*** END SCRIPT ***
After a night of reflection, I found no issue.
My script works fine. It seems that Neoload did not compile the code.
Because I changed nothing today and the variable returned the expected value.
Nevertheless, I am now stuck because the variable returned is null on "POST parameters".
I don't understand because on URL path parameters, the value is good.
Please advise.
Forget it.
I called script too early.
The solution are to insert an action to get the variables before using the script.
Thanks.
Update: Ironically that's what I replaced 'myvalue' with exactly. I used 'max' during my tshooting of your script. I actually was on my home computer so I didn't have the context.variableManager.getValue function live (i.e. running it within NeoLoad) but I just used 'alerts' for vars at different stages of your script to see what and where you might be losing passing any data. The only other thing I noticed like I had said was some vars return as 'undefined' based on the order they are declared and how they are declared within the evaluate function. Each 'max' 'text' 'i' and value vars really should be cleanly and safely declared outside and above of the function itself. Doing so prevents any scenario where when called any one might return as 'undefined' which then causes your entire function to fail and thus also your final 'text' var returns empty/undefined/or Null as well.
Best practice would be to make sure to declare all the variables at the start of a script. Especially avoid declaring them within a function that uses them itself where the actual defining of the vars can re-occur several times and can also cause unexpected and undesirable results.
First response from David:
Eric, just out of curiosity as I was looking at this script yesterday you aren't declaring the 'myvalue' var and it's also not the best thing to do to declare vars within a function or it's repeatedly declaring them in your loop, best to set them before the function and just call them within the function to avoid memory leaks or slow performance. Just my 2 cents. (David)
Hello David,
Sorry, I forget to replace the value "myvalue" by "max" in my example.
My purpose was to add a "," until the last variable was added.
Thanks.