1

I try to Loop scenario for 2 times fetching data from array.

My use case is: I want to create user, for multiple countries, so instead of coping the case, I want to implement loop logic.

So far a tried like, but without success:

  1. from here I want to fetch data

//JSR223 PreProcessor

String[] varArray = {"US", "UK"};
idx = Integer.parseInt(vars.get("loopCounter"))-1;
vars.put("myVariable", varArray[idx]);

enter image description here

  1. counter logic: enter image description here

  2. Whole script, which is chain of multiple API calls, and i want to use fetch data, only a single call.

enter image description here

Result: I got null, if I try to use fetch variable.

Script37.groovy: 1: unexpected token: US @ line 1, column 22.
   String[] varArray = {"US", "UK"};
1
  • 1
    just edit my post, i got null for the fetched variable. Commented Oct 7, 2019 at 11:39

2 Answers 2

1
  1. Define countries variable via User Defined Variables like:

    enter image description here

  2. In the Loop Controller you can dynamically get the array length via __groovy() function as:

    ${__groovy(vars.get('countries').split().size(),)}
    

    enter image description here

  3. Wherever you want inside the Loop Controller you can reference the "current" country for the given loop as:

    ${__groovy(vars.get('countries').split()[vars.get('__jm__Loop Controller__idx') as int],)}
    

    enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use square brackets for java array when using Apache Groovy:

String[] varArray = ["US", "UK"];

If you were using Beanshell/Java scripting language, you code will work

String[] varArray = {"US", "UK"};

7 Comments

OK, error is gone. Just test is not run two times (based on two fetched variable) What should be used in Loop controller variable field?
@vlatko606 ${loopCounter} in Loop Count
@vlatko606 didn't my answer helped? Loop count can be a number 2 in your case
No, i still did not managed to tell 'Loop Controller' how much times to run.If i use ${loopCounter}, my 'Loop Controller' is not running at all. IF i use static number into 'Loop Controller' ex:2, it runs 2 times. What i am doing wrong?
vars.put("myVariable", String.valueOf( varArray.length));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.