0

I have JDBC request that returns a string like: "this is an example". I want to be able to split that string by character (i.e: var_1=t, var_2=h, var_3=i, etc..) and the iterate them in a HTTP Request. Not only iterate, but increment the search pattern every request and then decrease at the end like:

  1. request: t
  2. request: th
  3. request: thi
  4. .....
  5. request: thi
  6. request: th
  7. request: t

right now I am using a thread group composed of jdbc request, jsr223 sampler(I've tried with beanshell sampler also but with no success either..), a forEach controller to iterate trough the values, a http request and a response assertion. I haven't been able to make it work because I got stuck at the jsr223 configuration. I guess the right beanshell script would fix this but I suck at groovy script..

isus hristos
  • 130
  • 1
  • 9

1 Answers1

1

Add a BeanShell Sampler after your JDBC request with the below code in the code area:

String MyVar = vars.get("MyVar");\\ MyVar is the name of the variable that hold the string returned from your JDBC request.
int x = MyVar.length();

for(i=3;i<=MyVar.length();i++){
    vars.put("Var_" + (i-2), MyVar.substring(0,i));
    vars.put("Var_" + (x * 2 - i -1), MyVar.substring(0,i));

}

Then configure your ForEach Controller as below:

  • Input variable prefix: Var
  • Output variable name: variable

Now you can use ${variable} in your HTTP request which will hold the values you want.

ararar
  • 953
  • 7
  • 19
  • Thank you so much! It works great. Had some problems initialy with some "Error invoking bsh method: eval Sourced file: inline evaluation" but I fixed it..I know I'm pushing it..but would it be possible to modify it so it starts from the first 3 chars instead of the first one? example: instead of starting with 1 then 2 then 3 etc..to start with 123 then 1234 then 12345 and finishing with 123 again? tnx – isus hristos Apr 04 '18 at 13:02
  • 1
    Edited the beanshell to start from the third character – ararar Apr 04 '18 at 13:15
  • just found it myself but tnx for all your help! – isus hristos Apr 04 '18 at 13:19
  • It seems I do have some problems..in any of the cases it does not execute the string twice..and it must execute twice..one problem could be the fact that I don't have anything specified in the forEach controller at "End index for loop(inclusive)". I dunno if that is of any importance but the sampler just stops when it reaches full string and does not progress back to have again 3 characters..p.s. would it be possible to put a conditional that the 3rd character should never be whitespace? tnx – isus hristos Apr 04 '18 at 14:02
  • 1
    I edited the answer again, please try it and let me know if you have errors, if it works i will check about the whitespace – ararar Apr 04 '18 at 14:18
  • Great!Now it works as intended. Any way to slip that "no whitespace in the initial 3 char variable" condition? tnx – isus hristos Apr 04 '18 at 14:25
  • So you want to remove whitespaces from all of the variables? or in case there was a whitespace only in the first 3 characters it should be removed and replaced by the 4th character ? or only remove it without replacing it (making it 2 characters instead of 3) ? – ararar Apr 04 '18 at 14:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168229/discussion-between-isus-hristos-and-ararar). – isus hristos Apr 04 '18 at 14:32