Today, while I am solving a old script, I need to remove a comma at the end in a string which is feed to construct a dynamic url in a request.
The whole string was constructed by a beanshell post processor in JMeter and I dont want to poke my nose there to debug the logic. The issue I found was the output string created by the beanshell script is not in correct JSON format. After analyzing that I found to trim a comma at the end. so Instead of adding my code at the end of the beanshell, I added as part of a preprocessor to the new request.
I attempted to use StringUtils class from apache library, but found it was not working. Before and after text processing, the number of the characters remains the same.
//StringUtils.substring(Final, 0, Final.length() - 2);
//StringUtils.chop(Final);
I spend an hour , but no luck. So I moved to a JSR223 preprocessor and choose Groovy as scripting language. I tried my luck with minus function and index function on the string and it worked successfully.
Then I attempted substring function like below and it worked fine . //Final.substring(0,2); so finally solved my issue by writing a substring in Groovy.
import java.lang.*;
import java.util.*;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3.StringUtils;
log.info("Final Results before trimming comma is "+vars.get("FinalQuery"));
Final = vars.get("FinalQuery");
//log.info("last ch is "+Final[-1]);
//log.info("fi is "+Final.substring(0,Final.length()-1));
Final =Final.substring(0,Final.length()-1);
//log.info("len is "+Final.length());
//Final.substring(0,2);
//StringUtils.substring(Final, 0, Final.length() - 2);
//StringUtils.chop(Final);
log.info("Final Results after trimming comma is "+Final.toString());
//log.info("len is "+Final.length());
vars.put("FinalQuery",Final.toString());
No comments:
Post a Comment