JMeter functions and variables - groovy

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

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.

merging two string variables results in empty value

I'd like to merge two string variables (STRING_VAR1 and STRING_VAR2) into one string variable STRING_ALL, such that the content of STRING_VAR1 or STRING_VAR2 is copied into STRING_ALL depending on which of those two variables contain any data (see example_dataset). If both variables STRING_VAR1 and STRING_VAR2 contain missing cases, STRING_ALL should be missing as well.
I've tried CONCAT (see code below) but that doesn't work for some reason and leaves me with only empty cases for STRING_ALL.
STRING STRING_ALL(A4)
COMPUTE STRING_ALL = CONCAT(STRING_VAR1, STRING_VAR2)
Thanks in advance!
Eli's suggestion gave you the necessary information to solve this specific issue. If you want to know why, check the Command Order topic in the SPSS Statistics Command Syntax Reference. It discusses the different types of commands and the fact that some of them, such as COMPUTE, do not take effect immediately, but are stored pending execution of a command that causes a data pass.

Dialogflow: if condition based on time response

I am making a pizza delivery chatbot and one of the training samples is
Are you open right now?
PARAMETER NAME ENTITY RESOLVED VALUE
time #sys.time now
One of the responses I want to have is if the time is between 12 am and 6 am , the reponse I coded is :
{{#if $time> 12:00:00}}I think it is too late{/if}}
But the response i am getting is :
{#if 12:00:34> 12:10:00}I think it is too late{/if}, which is wrong. Can someone help me on how to resolve this.
TIA
The Response section has a very very simple templating system - it allows for parameter/value replacement, and that is about it.
So you can specify a parameter value from an input phrase using something like $parameter-name, while parameter values in a Context or Event would be #context-name.parmaeter-name. You don't need the {braces} to do the evaluation, since those are used to escape the $ and # as special characters. So if you needed to show "$100", you would write that as ${100}. If you need the braces to be displayed, you'd include those inside another set of braces, which is why your text seems to get the braces removed.
You will need to put this processing in your fulfillment code. Libraries such as multivocal will let you create responses and setup response logic using templates. (Multivocal uses the handlebars templating library, for example.)

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:

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