3

What I have to do:
1. Extract all customer ids from previous response.
2. Shuffle all ids.
3. Pass all ids in one request. (like : custPref - 9768,7651,3215,....)

I took all customer ids in one variable (custID) using regular expression (with set match no. = -1)

By using For Each controller, I am able to pass one customer id in one request.
But now I have to pass all customer ids in one request to set the preferences of customers after shuffle the customer ids with comma separated values.

Also, count of customer Ids are not fixed so could not use variable as ${custID}_g1, ${custID}_g0...

Can you please suggest any way to shuffle customer ids and pass all ids in one request.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Jewel
  • 33
  • 1
  • 3
  • 2
    Please show us some code. What do you have tried? – Bergi Dec 08 '12 at 18:44
  • Regular expression : for="(.[0-9]*)" – Jewel Dec 08 '12 at 18:57
  • Refrence name: custID Regular expression : for="(.[0-9]*)" template: $1$ and match no. : -1 But if I use ForEach controller to pass custID, It creates multiple requests - total number of custID_matchNr.. But I need to send all custID in single request to set preferences of customers – Jewel Dec 08 '12 at 19:07

2 Answers2

3

Hint: you can get number of customer Ids using custID_matchNr.

So your complete Beanshell script may look like:

import java.util.ArrayList;
import java.util.Collections;

ids = new ArrayList();
idCount = Integer.parseInt(vars.get("custID_matchNr"));
for (int i=0; i<idCount; i++){
  ids.add(vars.get("custID_" + String.valueOf(i+1)));
}

Collections.shuffle(ids);

builder = new StringBuilder();
for (String id: ids){
  builder.append(id);
  builder.append(",");
}
builder.deleteCharAt(builder.length()-1);
vars.put("custPref", builder.toString());
Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123
  • Thanks to reply!! I tried this code in bean shell post processor and passed variable as: ${custPref} but customer ids are not going with request. And error in log file is : ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: `` builder = new StringBuilder(); for (int i=0; i – Jewel Dec 08 '12 at 19:30
  • @garima I've fixed it in first edit. It was because of excess parentheses. In second edit I aimed to provide full solution. – Andrei Botalov Dec 08 '12 at 19:32
  • value sending in request is "${custID} " instead of ids values. when I did print the values of ids and custID in log file, it gives [null3, null9, null2, null5, null1, null6, null0, null8, null7, null4] and void respectively. Regular expression is : '${__regexFunction(for="(.[0-9]*)",$1$,ALL,id7)}' And Debug sampler shows custID: 'custID_1=24358 custID_10=24361 custID_10_g=1 custID_10_g0=for="24361" custID_10_g1=24361 custID_1_g=1 custID_1_g0=for="24358" custID_1_g1=24358 custID_2=24364 custID_2_g=1 custID_2_g0=for="24364" custID_2_g1=24364 ' – Jewel Dec 08 '12 at 20:07
  • structure of requests: `Test Plan + Thread Group + Login Get + Login Post -- extracting custID + JSON request 1 + JSON request 2 + Save -- Passing custID and other variable ` – Jewel Dec 08 '12 at 20:22
  • shuffling code is working fine. However, values going with request are: null4,null2,null0,null3,null7,null1,null9,null6,null5,null8, instead of 24358,24364,24357,24363.... – Jewel Dec 08 '12 at 20:40
  • @garima Strange. Have you tried it with last version of code in the post? – Andrei Botalov Dec 08 '12 at 20:43
  • oops, was trying with earlier code.. This time getting values: null,24359,24357,24360,... we are getting one value as "null" here.. Is there any way to collect all customer ids and then only few will set as pref. Example: we have 100 customer ids and want to set preferences for only top 10 customer. (I have check boxes to set preferences in UI) – Jewel Dec 08 '12 at 20:59
  • thanks for helping me!! one value is going as null in request. request sample : 24358,**null**,24365,24360,24359,24357, – Jewel Dec 08 '12 at 22:43
  • @garima I think you should learn programming to fix such issues yourself. It was mistake in code. [Docs](http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor) say that `n = 1,2,3`. I haven't noticed it – Andrei Botalov Dec 09 '12 at 08:27
0

If you need to pass array of ints via jmeter to web method, or something similar, here is the solution!

  1. In http request use Post Body.

  2. In post body you need to pass json! Like so: {"language":"en", "translationIds":[10254, 3071, 3072, 3073, 3074, 3075, 3076, 3077, 3078, 3079, 3080, 3081, 3082, 3083, 3084, 3085, 3086, 3087, 3088, 3089, 3090, 3091, 3092, 3093, 3094, 3095, 3096, 3097, 3098, 3099, 3100, 3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108, 3109, 3110, 3111, 3112, 3113, 3114, 3115, 3116, 3117, 3118, 3513]}"

  3. You need HTTP Header Manager with: Content-Type application/json

Picture 1

Second image

(CODE) 4. And final you need to place [ScriptService] attribute (C#) on the class that contains the method.

DarkoM
  • 335
  • 4
  • 5
  • I tried this with PHP and couldn't get it too work. The JSON wasn't going into the POST variables. Now im sure I could do something like `json_decode(file_get_contents("php://input"));` but this wouldn't help me test the script in the same way it would work with ajax. – Lightbulb1 Mar 25 '14 at 10:20