0
votes

Hi All / Dimitri T Could you please post your valuable thoughts on extracting corresponding Values (For example ItemID1 and ItemSlot1) in one block of code randomly. I was able to write below Jsr223 postprocessor code and it is working fine. But when there are Blank spaces in ItemSlot id, then they are not fetching. From below code , i am passing ${rannum} under "Match No" in required regular expression.

Note: There will be more than 100 corresponding values. In some cases, we won't have ItemSlot1.i.e Blank/null values are appearing from server response. Hence, my script is not picking corresponding values.

Application Server Response:

"viewSaleListingLink": "https://Example.com/cars/item/search/-/listing/ItemID1/100011142", "saleCountry": "", "saleNote": "", "bidLiveUrl": "https://Example.com/cars//registration?p_p_id=RegistrationPortlet_WAR_PWRWeb&p_p_lifecycle=1&p_p_state=normal&ItemSlot1=103009468",

JSR223PostProcessor Code

import java.math.MathContext; import java.math.RoundingMode;

// Read occurance values from pervious response def Max = Integer.parseInt( vars.get("ItemID1_matchNr"));

int min=1;

int rannum = min + (int) (Math.random() * ((Max - min) + 1));

log.info("Values id ="+rannum);

vars.put("rannum",rannum.toString());

enter image description here

1

1 Answers

0
votes

If you need to extract a random match/pair of matches from the response using Regular Expression Extractor - it's sufficient just to provide 0 as the "Match No" and it will automatically fetch the random match group so you won't have to write any code:

enter image description here

Also be aware that Post-Processors are executed in the order they appear (upside down) so:

  • If your JSR223 PostProcessor is above the Regular Expression Extractor - ItemID1_matchNr will be undefined
  • If your JSR223 PostProcessor is below the Regular Expression Extractor - your rannum variable won't have any value

Also your response seems to be JSON so it makes sense switching to JSON JMESPath Extractor which is more powerful and convenient