Format the stirng value - string

I have the following value in a variable.
9.2.345.113619.2-218.98721247546.30582.1191562771895.2
I want to change the format of this value to 9-2-345-113619-2-218-98721247546-30582-1191562771895-2 and store it in another variable. Is there any function in JMeter that can do this?

Suppose your value is stored in Jmeter_variable
Then place one bean-shell sampler and write below mentioned code
value=vars.get("Jmeter_variable").replaceAll("\\.","-");
vars.put("Updated_variable",value);
Now use ${updated_variable}, where you want to use

Inline, using __javaScript() function
${__javaScript("9.2.345.113619.2-218.98721247546.30582.1191562771895.2".split(".").join("-");,)}
Against variable using __Beanshell function
${__BeanShell(vars.put("newStudyID"\, vars.get("").replaceAll("\\\\."\,"-")),)}
In any Beanshell Test Element:
vars.put("newStudyID", vars.get("").replaceAll("\\\\.","-"));
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using scripting in your JMeter test.

Related

Call groovy script dynamically in Apache Camel using doTry-doCatch

I'm building a route which calls a groovy script whose path is dynamically computed and, if the script can't be found, defaults to a generic, static script:
.doTry()
.toD("language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
The problem is that the exchange property consumerType is not resolved since the uri string parameter of toD is evaluated using groovy and not simple.
MultipleCompilationErrorsException -> startup failed:
Script_09b4150584d9e2c979353feee06897b5.groovy: 1: Unexpected input: 'scripts/${exchangeProperty.consumerType}' # line 1, column 20.
resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy
^
1 error
How can I obtain the desired behavior?
According to the error shown there, it seems Camel is not able to resolve the string you provided in the toD().
By default, the expression you pass to a dynamic to is evaluated as Simple language but, as described in To Dynamic Camel documentation, you can specify other languages for the dynamic evaluation.
In your case, you are trying to evaluate the endpoint with groovy language but then you're using Simple language to substitute a piece of the name of the script.
One solution I've found (yet not the best) would be to specify the language for the interpretation of the string as simple and then use language:groovy to specify the endpoint that will need to be called.
You could write something like this:
.doTry()
.toD("language:simple:language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
It seems to work, but I hope someone else comes up with a better solution.

JMeter functions and variables

I'm new to JMeter so this question may sound absolutely dumb...
I have a loop in which a variable (let's say it is called "raw") is being changed and written to file every iteration. The variable contains HTML encoded text so it has to be converted into plain text. I found out this can be done using __unescapeHtml function. When I tried using it worked but I ended up always receiving the same text as on the first iteration. Then I learned that I have to use vars.get instead of ${} to access a variable. So I changed ${__unescapeHtml("${raw}")} to ${__unescapeHtml(vars.get("raw")} which kind of helped: vars.get is getting the new value of raw each iteration but __unescapeHtml didn't work at all now - it just returns the encoded text from raw. I didn't succeded finding anything about this exact problem so I'm kind of stuck.
Ended up using
import org.apache.commons.lang3.StringEscapeUtils
...
StringEscapeUtils.unescapeHtml4(vars.get("raw"))
Don't know if it is a good way to do this but at least it works.
I assume, that you are using the expression ${...} inside a JSR-223 sampler or similar context. The user manual for JSR-223 Sampler states, that those scripts can be cached by JMeter. That is why you only get the values from the first time the context gets created.
The same is true for simple variable evaluations as ${varname}, as for function calls like ${__unescapeHtml(...)}.
The solution here is:
don't use ${...} inside of JSR-223 contexts, that might be cached.
you can however pass those expressions (${...}) into the context by using them as parameters through the input labeled Parameters on the JSR-223 Sampler – again assuming, that you are using it.
you can use the features, that your chosen JSR-223 context gives you, as you have done, by using the StringEscapeUtils#unescapeHtml4

JMeter - User defined counter not incrementing for a Loop

I am quite new to JMeter and I was trying to increment a counter variable pre-defined in User Defined Variables using a Loop Controller and a JSR223 PostProcessor and it seems not working well. I looked at various examples on JMeter loop and counter examples to work this out but the config element Counter was also not incrementing with the loop.
Could anybody please let me know what I am doing wrong?
This is the user defined variable counter:
This is how I do loop:
And this is how I am trying to increment the counter using post processor and the log displays that the counter is not incrementing for each loop. I want to get the counter upto 5:
[EDIT]
I guess I wasn't clear on why I used ${counter} to test out. I was trying to evaluate the counter variable within another variable as '${__V(transaction_${counter})}' because this needs to be appended to a text file. For example, if transaction_3 has a value "110001", if I do '${__V(transaction_'+vars.get('counter')+')}' to append, the text stored shows "transaction_3" and if I do '${__V(transaction_${counter})}' then I get the correct value "110001" stored in the text file while in next iteration the counter does not increment. Is there a possible way to solve this problem?
This is how I was using the variable within variable and it shows that the values are not changing because the counter is not changing.
And this image is how I am trying to do with vars.get() and it shows that it is just putting the variable name instead of evaluating the value of each item.
According to JSR223 Sampler documentation:
JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.
props.get("START.HMS");
props.put("PROP1","1234");
So amend the last line of your script to look like:
log.info(vars.get('counter'))
Demo:
Also be aware that it's much easier to use:
Counter test element or __counter() function, check out How to Use a Counter in a JMeter Test article for more details
Loop Controller exposes ${__jm__Loop Controller__idx} JMeter Variable which holds current iteration number
According to the JMeter Manual Best Practices -> 16.12 JSR223 Elements
When using JSR 223 elements, it is advised to check Cache compiled script if the available property to ensure the script compilation is cached if the underlying language supports it. In this case, ensure the script does not use any variable using ${varName} as caching would take only the first value of ${varName}. Instead use: vars.get("varName")`
If you change log.info('${counter}') to log.info('${vars.get("counter")}') that should do the trick!
Or uncheck this:

Passing a parameter from one thread to another in Jmeter

In Jmeter ,When logging in an application, Parameters like Viewstate are generated. I need to use this parameter in my next thread. ( I am not passing a variable but a parameter that has a "NAME" and "Value"). Can someone help please?!!
You can extract the parameter using regular expression extractor as :
Then use BeanShell Assertion to make that variable global.
use the variable wherever you want using __property function.
${__property(devicegroup_id)}

expression engine dynamic variable names: {slide_{index}_title}

I am using a simple looping plugin so that my template looks like this:
{exp:loop_plus start="1" end="4" increment="1"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
However, I am ending up with the following output:
<h3>{slide_1_title}</h3>
<h3>{slide_2_title}</h3>
<h3>{slide_3_title}</h3>
<h3>{slide_4_title}</h3>
Is there any way I can have dynamic variable names like this? I am not looking for alternative methods for building a slider, I simply would like to know if the dynamic variable names like this is possible. Thanks!
I'm assuming that Loop Plus (http://devot-ee.com/add-ons/loop-plus) sets the {index} part, so the question is what is defining {slide_1_title}...?
Assuming you have an entry field or variable with this defined, what you have is correct, but if it's not working, it means there's a parsing order issue.
Let's assume the code you supplied is wrapped in a {exp:channel:entries} tag pair, what happens is EE will try to parse the variable first, so will see: {slide_{index}_title} which doesn't exist. The {exp:loop_plus} add-on will then parse it, converting it to {slide_1_title} (but to late as channel:entries has already tried to parse it), which is what is finally output to the template.
So what you want to ensure is that EE parses {exp:loop_plus} before {exp:channel:entries}, do this using parse="inward" tag:
{exp:loop_plus start="1" end="4" increment="1" parse="inward"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
This is a global EE parameter that EE uses to control parse order - you won't find it documented under the specific add-on. By adding the parameter, it means this child tag will get parsed before it's parent.
One way you could do it is to declare a preload_replace variable in your template and use it in your custom field name.
So something like:
{preload_replace:my_var_prefix="whatever"}
And then in your loop, you could then use:
{slide_{my_var_prefix}_title}

Resources